Windows batch: Unicode parameters for (robo) copy command

19,752

Solution 1

If

  • I add CHCP 65001 as the first line of your batch file,
  • save the file as UTF-8 without BOM, and
  • set my console font to something else than "Raster Fonts" (on my Win7 box I can choose Consolas or Lucida Console),

it works. Simple, no? :-)

(The font change is actually not necessary, provided you're not writing non-ASCII output to the console.)

Solution 2

I'm not certain, but I think the short (8.3) filename will be ASCII, so you could refer to it that way? You can find out the short filename with dir /X .

Share:
19,752
Helge Klein
Author by

Helge Klein

Author of uberAgent for Splunk (user experience and application performance monitoring), Delprof2 (user profile deletion), SetACL and SetACL Studio (permissions management).

Updated on June 04, 2022

Comments

  • Helge Klein
    Helge Klein almost 2 years

    I need to copy multiple files in a single batch file. The files have Unicode names that map to different codepages.

    Example:

    set ArabicFile=ڊڌڵڲڛشس
    set CyrillicFile=щЖЛдЉи
    set GermanFile=Bücher
    
    copy %ArabicFile% SomePlaceElse
    copy %CyrillicFile% SomePlaceElse
    copy %GermanFile% SomePlaceElse
    

    Problem: Batch files cannot be Unicode.

    Question: How can I write the Unicode file names to the batch file so that the copy command recognizes them?

    Notes:

    I do not care how the file names are displayed.
    Actually the batch file does much more than just copy these files, I just simplified the description to make the problem clearer.

    Correct batch file:

    With Arnout's answer I modified my batch file as follows. It now works correctly without requiring a font change (which would be messy, as Arnout commented).

    @echo off
    
    chcp 65001
    
    set ArabicFolder=ڊڌڵڲڛشس
    set CyrillicFolder=щЖЛдЉи
    set GermanFolder=Bücher
    
    robocopy /e d:\temp\test\%ArabicFolder% d:\temp\test2\%ArabicFolder% /log:copy.log
    robocopy /e d:\temp\test\%CyrillicFolder% d:\temp\test2\%CyrillicFolder% /log+:copy.log
    robocopy /e d:\temp\test\%GermanFolder% d:\temp\test2\%GermanFolder% /log+:copy.log
    
  • Rich
    Rich over 13 years
    The font change can be necessary if you intend to use command output that may contain Unicode ;)
  • Arnout
    Arnout over 13 years
    Umm, yeah, that's what I meant with "provided you're not writing non-ASCII output to the console"...
  • Helge Klein
    Helge Klein over 13 years
    Good thinking! It cannot use your solution in my case, though, because the batch file needs to run on multiple systems and the short names may differ between the computers (for example when the order in which files were created was not identical).
  • Helge Klein
    Helge Klein over 13 years
    Cool, this works ;-) Only gotcha: how do I set the font from within a batch file? The batch file is to run on multiple computers and I cannot reconfigure them prior to running my script. And the font change is required - if I do not change the font, the set commands fail.
  • Arnout
    Arnout over 13 years
    Would adding an ECHO OFF do the trick, or do you depend on being able to see (some of) the output in the console window? You might be able to specify the font by importing a .REG with some settings under HKCU\Console, but that's pretty ugly and probably not very robust. (You would have to do that from another .CMD script, of course.)
  • Philipp
    Philipp over 13 years
    Short file name generation can be disabled, so you should in no case rely on them.
  • jahu
    jahu almost 10 years
    On my Win7 64bit machine CHCP was case sensitive and I had to write the command in lower case. Otherwise the command was not recognized.
  • curropar
    curropar over 7 years
    This doesn't work for my, and I need a list of all the individual folders and subfolders on my file server (more than 95000), with almost 9000 failing because of the accents. Funny thing is I switch to Robocopy from Powershell as I've as well problems with long paths. So kind of desperate by now...