Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

chcp 65001 and a .bat file

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.

like image 384
maro Avatar asked Aug 24 '15 12:08

maro


People also ask

What CHCP 65001?

chcp 65001 sets both InputEncoding and OutputEncoding to UTF8. [console]::OutputEncoding is affected as well.

How do I run a CHCP 65001?

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.

Is BAT file and batch file same?

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: *.

What is a .bat file?

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.


1 Answers

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

like image 200
Greg Wittmeyer Avatar answered Oct 16 '22 17:10

Greg Wittmeyer