chcp 65001和。bat文件

时间:2023-01-25 02:06:41

I have a problem with chcp 65001 command in Windows shell.

我对Windows shell中的chcp 65001命令有问题。

I need to generate a list of files in a folder. So I ran cmd.exe, typed

我需要在文件夹中生成文件列表。所以我跑cmd。exe,类型

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

它起作用了,但是我对一些文件名中的特殊的非ascii字符有问题。我加了chcp 65001

Everything worked, but when I put these commands into a .bat file, the script doesn't work.

一切都运行良好,但是当我将这些命令放到.bat文件中时,脚本就不能工作了。

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.

一切都在cmd工作。exe,但不在。bat文件中。

I've read the topic: *.com/questions/2182568/batch-script-is-not-executed-if-chcp-was-called, but it didn't help.

我读过这个主题:*.com/questions/2182568/批量脚本不是执行的if-chcp-被调用,但是没有帮助。

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更改为chcp 1250,因为所有字符都在这个编码中。但实际上这并不能回答问题。

5 个解决方案

#1


2  

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

使用cmd / U。见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:

大多数常见的文本文件是ANSI,当您需要转换字符集时使用这些开关。这些选项将影响管道或重定向到一个文件:

  • /A Output ANSI characters
  • ANSI /输出字符
  • /U Output UNICODE characters (UCS-2 Little Endian)
  • /U输出UNICODE字符(UCS-2小Endian)

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

这是我的尝试(当然是在cmd /A下启动):

@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:

输出。仍然无效的第一行(十六进制0D0A),抱歉;使用另一种方法获得纯Utf-8字节的顺序标记:

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

==>

#2


2  

"chcp 65001" does not work before Windows 7. It will cause the batch to terminate immediately. There is no work-around.

“chcp 65001”在Windows 7之前不能工作。这将导致该批产品立即终止。没有办法解决。

I have verified this by directly testing 2003, XP, Vista, 2008, 7, 8, and 10.

通过直接测试2003、XP、Vista、2008、7、8和10,我已经验证了这一点。

#3


0  

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

仅在Windows 7上测试,可能不能在Windows Vista上使用。

Apparently chcp doesn't affect dir directly.

显然chcp不会直接影响dir。

Parse the output of dir and print it via echo:

解析dir的输出并通过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.

注意:输出文件没有UTF-8字节的顺序标记。

#4


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

#5


0  

On Windows 2003 worked this:

Windows 2003的工作原理是:

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

C:\windows\* - only sample

C:\ windows \ *——只有样品

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

&& chcp 866 -默认代码页,允许继续批量处理。

#1


2  

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

使用cmd / U。见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:

大多数常见的文本文件是ANSI,当您需要转换字符集时使用这些开关。这些选项将影响管道或重定向到一个文件:

  • /A Output ANSI characters
  • ANSI /输出字符
  • /U Output UNICODE characters (UCS-2 Little Endian)
  • /U输出UNICODE字符(UCS-2小Endian)

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

这是我的尝试(当然是在cmd /A下启动):

@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:

输出。仍然无效的第一行(十六进制0D0A),抱歉;使用另一种方法获得纯Utf-8字节的顺序标记:

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

==>

#2


2  

"chcp 65001" does not work before Windows 7. It will cause the batch to terminate immediately. There is no work-around.

“chcp 65001”在Windows 7之前不能工作。这将导致该批产品立即终止。没有办法解决。

I have verified this by directly testing 2003, XP, Vista, 2008, 7, 8, and 10.

通过直接测试2003、XP、Vista、2008、7、8和10,我已经验证了这一点。

#3


0  

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

仅在Windows 7上测试,可能不能在Windows Vista上使用。

Apparently chcp doesn't affect dir directly.

显然chcp不会直接影响dir。

Parse the output of dir and print it via echo:

解析dir的输出并通过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.

注意:输出文件没有UTF-8字节的顺序标记。

#4


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

#5


0  

On Windows 2003 worked this:

Windows 2003的工作原理是:

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

C:\windows\* - only sample

C:\ windows \ *——只有样品

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

&& chcp 866 -默认代码页,允许继续批量处理。