13

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.

Community
  • 1
  • 1
maro
  • 473
  • 4
  • 11
  • 30
  • What happens when you put these four commands into a batch file and run it? `cd folder` `chcp 65001` `dir /B /O:N` `pause` – foxidrive Aug 24 '15 at 13:00
  • foxdrive: the cmd window opens for a short while and immediately closes – maro Aug 24 '15 at 13:11
  • did you try it with the `pause` command on line 4? – foxidrive Aug 24 '15 at 13:16
  • Yes. It didn't pause. I also tried `timeout 5`. But when I change 65001 to other valid encoding, everything works. – maro Aug 24 '15 at 13:22
  • If you paste this into a cmd window - what do you see? `chcp 65001 & dir /B /O:N & pause` – foxidrive Aug 24 '15 at 13:26
  • I see that enconding was changed, a list of files and Press any key to continue. But if I add `> list.txt` it does generates the list but without unicode characters (as I wrote in the question in the example). – maro Aug 24 '15 at 13:39
  • You said `the script doesn't work` and `doesn't generate the list.` and it wasn't clear what you were doing or what the result was from the script. Because the pause didn't work it looked like your batch file name was causing a problem. – foxidrive Aug 24 '15 at 13:49
  • Related. • [What encoding to get Å Ä Ö to work](https://superuser.com/q/675369) • [Using UTF-8 Encoding (CHCP 65001) in Command Prompt](https://stackoverflow.com/q/57131654) • [How to use unicode characters in Windows command line](https://stackoverflow.com/q/388490) • [chcp 65001 and a .bat file](https://stackoverflow.com/q/32182619) • [Making Swedish characters show properly in Windows Command Prompt](https://stackoverflow.com/q/2660264) – Henke Jan 31 '23 at 15:53

7 Answers7

8

"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.

Greg Wittmeyer
  • 439
  • 4
  • 14
  • There is exist a binary patch for `Windows XP`: http://consolesoft.com/p/cmd-xp-65001-fix/ But seems it is only for `Windows XP 32-bit`. – Andry Dec 13 '18 at 08:27
5

Use cmd /U. See http://ss64.com/nt/cmd.html:

Most common text files are ANSI, use these switches when you need to convert the character set. These options will affect piping or redirecting to a file:

  • /A Output ANSI characters
  • /U Output UNICODE characters (UCS-2 Little Endian)

Here's my attempt (launch it under cmd /A, of course):

@ECHO OFF >NUL
SETLOCAL EnableExtensions

:: create a UNICODE file with Byte Order Mark using `wmic` 
chcp 852 >NUL
>list_of_files.txt wmic os get localdatetime

:: store a line with BOM to a variable
:: although FINDSTR does not support UTF-16 files
:: it will read first three bytes at least
for /F "delims=" %%G in ('
    findstr "^" list_of_files.txt
  ') do set "UTF8BOM=%%G"

:: write BOM only* to a file (* echo writes hexadecimal value FFFE0D0A)
:: the `<NUL set /p =text` trick does not work: chokes down leading `FF`  
>list_of_files.txt echo(%UTF8BOM:~0,2%

chcp 65001 >NUL
:: add CRLF in  Unicode (hexadecimal 0D000A00)
>>list_of_files.txt cmd /U /C echo(

:: add result of `dir /B /O:N` in Unicode 
>>list_of_files.txt cmd /U /C dir /B /O:N

:: check the result: still invalid first line, see output
type list_of_files.txt
chcp 852 >NUL

Output. Still invalid first line (that hexadecimal 0D0A), sorry; use another method to get pure Utf-8 byte order mark:

==>cmd /A /C D:\bat\SO\UTF8BOM32182619.bat
਍
cpANSI_OoCcSsUu.txt
cpANSI_ÖöÇ窺Üü.txt
escrzyaie.txt
ěščřžýáíé.txt
list_of_files.txt

==>
JosefZ
  • 28,460
  • 5
  • 44
  • 83
  • 1
    But the user doesn't use cmd.exe, he just run the .bat file, so how can I use `/U` in a .bat file? – maro Aug 25 '15 at 09:47
  • 1
    Why don't you _read_ the batch script provided? There is `cmd /U` used _twice_... And what do you mean by _he just **run** the .bat file_? Double-click it? Just take a look in Task Manager: _any_ .bat script is always performed in a `cmd` instance... – JosefZ Aug 25 '15 at 13:11
  • If you want a BOM in the beginning of a file, just make template files like `efbbbf.bin` or `feff.bin` with respective characters in a notepad like `notepad++` and just redirect it into your file: `type efbbbf.bin > "file_with_bom.txt"` – Andry Dec 13 '18 at 07:22
2

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:

chcp 1252
cd c:\My Folder\Filename_with_åäö-characters.txt

Now it should work.

Peter S
  • 31
  • 5
1

On Windows 2003 worked this:

chcp 65001 && cmd /C dir C:\WINDOWS\* && chcp 866

C:\windows\* - only sample

&& chcp 866 - default code page and this allow to continue batch

dur
  • 15,689
  • 25
  • 79
  • 125
SolarWind
  • 11
  • 2
0

Tested on Windows 7 only, may not work on Windows Vista.

Apparently chcp doesn't affect dir directly.

Parse the output of dir and print it via echo:

chcp 65001
>list_of_files.txt (for /f "delims=" %%a in ('dir /B /O:N') do echo %%a)

Note: the output file won't have UTF-8 Byte Order Mark.

wOxxOm
  • 65,848
  • 11
  • 132
  • 136
  • Please elaborate. It works on Win7SP1x64 English here. – wOxxOm Aug 24 '15 at 13:39
  • Did you put this into a .bat file? I have Windows Vista 32bit. – maro Aug 24 '15 at 13:53
  • 1
    Yes, it's in the batch file, otherwise `%%` wouldn't work. Blaming Vista is the obvious choice, it's notorious for hard-to-discover bugs and underdeveloped features. – wOxxOm Aug 24 '15 at 13:55
0

it looks like a problem I recently met

cd folder
dir /B /O:N > list_of_files.tmp
cmd /U /C type list_of_files.tmp>list_of_files.txt
del list_of_files.tmp
Community
  • 1
  • 1
Paul
  • 2,620
  • 2
  • 17
  • 27
0

Did you try to change the defaults for cmd?
You can control the code page simply by creating a file %HOMEPATH%\init.cmd.
This sets defaults for cmd.exe.
Mine says:

@ECHO OFF
CHCP 65001 > nul
user333869
  • 549
  • 1
  • 4
  • 13