Batch file “批处理中丢失Unicode字符”;至于;环

Batch file “批处理中丢失Unicode字符”;至于;环,batch-file,unicode,Batch File,Unicode,在批处理文件中将Unicode字符(在本例中是卷引号)转换为ASCII字符时,我遇到了for循环的困难 当然,这是一个简化的示例——但为了说明这一点,我在测试文件夹中创建了两个文件: 普通文件名.txt 带“引号”的文件名.txt 我在该文件夹中创建了一个批处理文件(Test.bat),以便使用xcopy遍历这些文件。测试.bat的内容如下: echo off chcp 65001 cls echo In this xcopy output, the curly quotes in fil

在批处理文件中将Unicode字符(在本例中是卷引号)转换为ASCII字符时,我遇到了
for
循环的困难

当然,这是一个简化的示例——但为了说明这一点,我在测试文件夹中创建了两个文件:

  • 普通文件名.txt
  • 带“引号”的文件名.txt
我在该文件夹中创建了一个批处理文件(Test.bat),以便使用
xcopy
遍历这些文件。测试.bat的内容如下:

echo off
chcp 65001
cls

echo In this xcopy output, the curly quotes in file names are preserved:
rem Nothing is actually copied by the xcopy command because of the /L flag.
xcopy "." "C:\" /L
echo:

echo But in the for loop over the exact same output, the curly quotes are are converted to normal quotes:
for /f "tokens=*" %%a in ('xcopy "." "C:\" /L') do (
    echo %%a
)
echo:

pause
当我执行Test.bat时,我得到以下输出:

In this xcopy output, the curly quotes in file names are preserved:
.\File Name with “Quotes”.txt
.\Normal File Name.txt
.\Test.bat
3 File(s)

But in the for loop over the exact same output, the curly quotes are are converted to normal quotes:
.\File Name with "Quotes".txt
.\Normal File Name.txt
.\Test.bat
3 File(s)

Press any key to continue . . .
请注意,Test.bat是使用UTF-8编码保存的,它包括
chcp 65001
命令,用于正确处理Unicode字符。的作者似乎也有类似的问题——但在我的示例中,这里给出的解决方案都不起作用


为什么卷曲引号会在
for
循环中丢失?我能做些什么来保存它们吗?

在将输出写入管道时,xcopy.exe硬代码使用OEM代码页(通过
ntdll!RtlUnicodeToOemN
),而
for/f
更灵活,因为它使用控制台的当前代码页进行解码。据我所知,在这种情况下,您无法更改xcopy.exe的行为。为什么要使用
xcopy
列出文件?为什么不使用
dir/B/A:-D.
xcopy
dir
有不同的功能。我在简化的示例中没有说明这些差异的事实并不表明它们与我的现实场景无关。由于我提出的问题是关于
xcopy
,为了避免其他人在后面提到这个问题时感到困惑,我想将讨论范围限制在
xcopy