Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows batch: Unicode parameters for (robo) copy command

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
like image 925
Helge Klein Avatar asked Nov 16 '10 10:11

Helge Klein


People also ask

What is Robocopy in batch file?

Robocopy is a robust file copying program built into Windows similar to UNIX rsync. It is a much better method of copying large datasets or lots of files across volumes and is a great tool for backing up data. It has the ability to resume copies if interrupted, various options and logging during copying.

Is Robocopy faster than XCopy?

The average Disk Read Transfer is better for XCopy (76.15 MB/Sec vs. 75.28 MB/Sec), the minimum Disk Read Transfer is better for Robocopy (4.74 MB/Sec vs. 0.00 MB/Sec) and the maximum Disk Read Transfer is better for XCopy (218.24 MB/Sec vs. 213.22 MB/Sec).

What does Robocopy xx do?

/XX (exclude extra) If used in conjunction with /Purge or /Mir, the exclude extra switch will take precedence and prevent any files being deleted from the destination.


2 Answers

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.)

like image 139
Arnout Avatar answered Oct 21 '22 21:10

Arnout


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 .

like image 41
Vicky Avatar answered Oct 21 '22 21:10

Vicky