I have a problem with chcp 65001
command in Windows shell.
I need to generate a list of files in a folder. So I ran cmd.exe, typed
cd folder
dir /B /O:N > list_of_files.txt
It worked, but I had a problem with special, non-ASCII characters which are in some file names.
So I added
chcp 65001
Everything worked, but when I put these commands into a .bat file, the script doesn't work.
So
cd folder
chcp 65001
dir /B /O:N > list_of_files.txt
doesn't generate the list.
and
cd folder
chcp 65001 && dir /B /O:N > list_of_files.txt
as well as
cd folder
chcp 65001 > nul && dir /B /O:N > list_of_files.txt
generates the list, but with the default encoding :/.
Everything works in cmd.exe, but not in .bat files.
I've read the topic: stackoverflow.com/questions/2182568/batch-script-is-not-executed-if-chcp-was-called, but it didn't help.
EDIT:
I partially solved my problem, changing chcp 65001
to chcp 1250
because all characters were in this encoding. But actually this doesn't answer the question.
chcp 65001 sets both InputEncoding and OutputEncoding to UTF8. [console]::OutputEncoding is affected as well.
You can put the command chcp 65001 in your Powershell Profile, which will run it automatically when you open Powershell. However, this won't do anything for cmd.exe. Microsoft is currently working on an improved terminal that will have full Unicode support.
Microsoft's Windows operating system typically uses the . bat filename extension for batch files. However, other types of batch files are also available in Windows, including: *.
A BAT file is known as a batch file runs with DOS and all versions of Windows, under cmd.exe. It consists of a series of line commands in plain text to be executed by the command-line interpreter to perform different tasks, such as running maintenance utilities within Windows or starting typical programs.
"chcp 65001" does not work before Windows 7. It will cause the batch to terminate immediately. There is no work-around.
I have verified this by directly testing 2003, XP, Vista, 2008, 7, 8, and 10.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With