.bat: Convert ANSI to UTF-8 with CHCP 65001
brucine Posted messages 24910 Registration date Status Member Last intervention -
Hello,
Until now, I was using the ANSI format for ".bat" files. But recently, I discovered compatibility issues due to special characters. I used (Alt XXXX) to correctly convert accented letters (file name, Start menu path...).
While searching online, I found a solution which consists of adding CHCP 65001 at the beginning of the code and putting the accents back as they are without (Alt XXXX) to save in UTF-8 format.
My question is, having a certain number of ".bat" files that I do not want to retest one by one, can using CHCP 65001 create disturbances, errors... in .bat files that were working perfectly in ANSI + (Alt XXXX)?
Thank you for your opinions and, if necessary, clarifications.
2 answers
-
Hello,
You need to clarify how you proceeded; it's not clear.
Assuming the following test:
echo menu dALT 130marrerpause
I am unable to obtain the correct display when the bat or cmd file is saved either as ANSI or UTF8 as soon as the ASCII character has been used; it is only obtained if I add the CHCP 65001 sequence to the ANSI file and save it as UTF8.
The lesson is therefore that CHCP 65001 is not enough and that all files need to be batch converted from ANSI to UTF8, see for example here, replacing of course unicode with utf-8 and csv with bat or cmd.
https://stackoverflow.com/questions/44157064/need-to-batch-convert-a-large-quantity-of-text-files-from-ansi-to-unicode
Additionally:-If I don't want to see the display, enter CHCP 65001>NUL
-In a number of cases (for example, ROBOCOPY), it is the command itself that returns exotic characters even in the absence of accents; then you need to enter CHCP 1252>NUL, which you can alternate as needed with 65001 in your command file.
-
Moreover, if the question is to modify all the files without opening them, I will indeed have to open them one by one in order to re-save them to add
CHCP 65001>NUL.
The catch is that if I want to suppress the display of this command, it should not occur on the first line of each file but on the second after the @echo off command.
The question is easy to solve with a utility and/or an appropriate command in the Linux world, much less so in plain Windows batch scripting.
One could possibly draw inspiration from something like this, but it might be quicker to manually modify each file at the time of use.
https://msfn.org/board/topic/118835-prepend-or-interpend-text-to-a-text-file-via-batch-script/
-
-
Hello,
Thank you for the response.
Let me try to clarify a bit more:
1) I use Notepad and type the necessary code, making sure to replace special characters using the "ALT + (code)" method. For example:
Call "C:\ProgramData\Microsoft\Windows\Start Menu\Documents to verify.lnk"
I replace "à" with "ALT + 133"
and I replace "é" with "ALT + 130"
This results in:
Call "C:\ProgramData\Microsoft\Windows\Start Menu\Documents … v‚rifier.lnk"
Saved in ANSI, this works perfectly on a French Windows.
> For compatibility reasons, I need to change my method.
2) Obviously, I have to open each ".bat" file to make the modifications: adding CHCP 65001 + adding special accents without modification + encoding in UTF-8. However, I do not want to execute them one by one to check that the operations are carried out without errors.
In short, the main question being:
Could "CHCP 65001" + "UTF-8" create execution issues for the .bat code (call, start, rd, variable...) compared to my ANSI base?
If not, the solution works for me.
* I do not wish to use PowerShell.
-
Hello,
I understood well, but as mentioned earlier, without CHCP 65001 I can't reproduce the behavior: if I enter ALT characters, the display is not correct whether it's saved as ANSI or UTF-8, this may be due to the system font of the console depending on the version of the operating system.
It becomes correct if I execute:
@echo off CHCP 65001>NUL echo "C:\ProgramData\Microsoft\Windows\Start Menu\Documents ALT 133 vALT 130.lnk" pause
saved as UTF-8 and it has no impact on the commands since the paths are read correctly.
Since the effect seems version dependent, I invite you to test by creating the above batch, if it works for one, it will work for all.
By the way, and if applicable in other situations than batches, ASCII characters including spaces are a source of troubles: in this situation, I prefer to name my file
Documents_a_verifier
-