0

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?

Eryk Sun
  • 33,190
  • 5
  • 92
  • 111
Joe DeRose
  • 3,438
  • 3
  • 24
  • 34
  • 1
    When writing output to a pipe, xcopy.exe hard codes using the OEM codepage (via `ntdll!RtlUnicodeToOemN`), whereas `for /f` is more flexible because it decodes using the console's current codepage. As far as I know, there's nothing you can do to change the behavior of xcopy.exe in this case. – Eryk Sun Jul 07 '17 at 19:58
  • 1
    Why are you using `xcopy` to list files? why don't you use `dir /B /A:-D .`? – aschipfl Jul 08 '17 at 08:09
  • 1
    `xcopy` and `dir` have different capabilities. The fact that I didn't illustrate those differences in my simplified example does not indicate that they are not relevant in my real-world scenario. Since the question I posed was about `xcopy`, to avoid confusion for others who may refer to this question later, I'd like to limit the scope of the discussion to `xcopy`. – Joe DeRose Jul 08 '17 at 20:46

0 Answers0