I'm having difficulty with a for
loop in a batch file that is converting Unicode characters (in this case, curly quotes) to ASCII characters.
This is a simplified example, of course -- but to illustrate I have created a two files in a test folder:
- Normal File Name.txt
- File Name with “Quotes”.txt
And I have created a batch file (Test.bat) in that folder to iterate through the files using xcopy
. The contents of Test.bat are:
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
When I execute Test.bat, I get the following output:
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 . . .
Note that Test.bat is saved with UTF-8 encoding, and that it includes the chcp 65001
command for proper handling of Unicode characters. The author of this question seemed to be having a similar problem -- but none of the solutions presented there worked in my example.
Why would the curly quotes be lost in the for
loop? And is there anything I can do to preserve them?