Windows 批处理:(robo)复制命令的 Unicode 参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4193413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:36:35  来源:igfitidea点击:

Windows batch: Unicode parameters for (robo) copy command

windowsunicodebatch-filefilenames

提问by Helge Klein

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

我需要在一个批处理文件中复制多个文件。这些文件具有映射到不同代码页的 Unicode 名称。

Example:

例子:

set ArabicFile=???????
set CyrillicFile=щЖЛд?и
set GermanFile=Bücher

copy %ArabicFile% SomePlaceElse
copy %CyrillicFile% SomePlaceElse
copy %GermanFile% SomePlaceElse

Problem:Batch files cannot be Unicode.

问题:批处理文件不能是 Unicode。

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

问题:如何将 Unicode 文件名写入批处理文件,以便复制命令识别它们?

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

通过 Arnout 的回答,我修改了我的批处理文件,如下所示。它现在可以正常工作而无需更改字体(正如 Arnout 评论的那样,这会很混乱)。

@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

回答by Arnout

If

如果

  • I add CHCP 65001as 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),
  • 我添加CHCP 65001为批处理文件的第一行,
  • 将文件保存为没有 BOM 的 UTF-8,以及
  • 将我的控制台字体设置为“光栅字体”以外的其他字体(在我的 Win7 机器上,我可以选择 Consolas 或 Lucida Console),

it works. Simple, no? :-)

有用。简单,不是吗?:-)

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

(实际上不需要更改字体,前提是您没有将非 ASCII 输出写入控制台。)

回答by Vicky

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.

我不确定,但我认为短 (8.3) 文件名将是 ASCII,所以你可以这样引用它?您可以使用dir /X.

回答by Resistor

I want to create a batch file (e.g. RunThis.bat) which creates directories of names that can be Russians or others.

我想创建一个批处理文件(例如RunThis.bat),它创建的名称可以是俄罗斯人或其他人的目录。

Example:
When DOS Windows is open with prompt:

示例:
当 DOS Windows 以提示方式打开时:

D:\>md "Russia - Шпионка"

This work in command like and the name appear correctly.

这在命令中工作并且名称正确显示。

But if I try that using Notepad and save in ANSII, I can't.
So if I use again Notepad and save in UTF-8, it will work but with garbage characters.

但是,如果我尝试使用记事本并保存在ANSII.
因此,如果我再次使用记事本并以 UTF-8 格式保存,它将可以工作,但会使用垃圾字符。

RunThis.bat (Notepad save UTF-8), give garbage characters.

RunThis.bat(记事本保存UTF-8),给垃圾字符。

chcp 65001
set fn14="Russia - Шпионка"
md %fn14%

The problem with notepad it uses UTF-8 withBOM.

记事本的问题是它使用带有BOM 的UTF-8 。

To save the .bat using UTF-8 withoutBOM we must use editor like Notepad++.

要使用没有BOM 的UTF-8 保存 .bat,我们必须使用像 Notepad++ 这样的编辑器。

RunThis.bat (Notepad++ save UTF-8 – no BOM)

RunThis.bat (Notepad++ save UTF-8 – no BOM)

chcp 65001
set fn14="Russia - Шпионка"
md %fn14%

This time its work perfectly when we run “RunThis.bat” directly from explorer.exe

这次当我们RunThis.bat直接从explorer.exe