我在批处理文件中遇到for
循环时出现问题,该文件正在将Unicode字符(在本例中为弯引号)转换为ASCII字符。
这是一个简化的例子,当然 - 但为了说明我在测试文件夹中创建了两个文件:
我在该文件夹中创建了一个批处理文件( Test.bat ),以使用xcopy
遍历文件。 Test.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字符。 this question的作者似乎遇到了类似的问题 - 但是我的例子中没有一个解决方案有效。
为什么在for
循环中丢失卷曲引号?我有什么办法可以保护它们吗?