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.
On Windows 2003 worked this:
C:\windows\*
- only sample&& chcp 866
- default code page and this allow to continue batchit looks like a problem I recently met
Use
cmd /U
. See http://ss64.com/nt/cmd.html:Here's my attempt (launch it under
cmd /A
, of course):Output. Still invalid first line (that hexadecimal
0D0A
), sorry; use another method to get pure Utf-8 byte order mark:In Windows, make sure that file format of the actual file is same as codepage of the console (cmd.exe). Swedish characters åäö are used commonly.
In editor (in this case) Notepad++ hit "Encode>Convert to ANSI"
In script (batchfile) run same codepage as the file (Microsoft's 1252 ANSI version), File:
Now it should work.
Apparently
chcp
doesn't affectdir
directly.Parse the output of
dir
and print it viaecho
:Note: the output file won't have UTF-8 Byte Order Mark.
"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.