windows 使用 xcopy 复制多个文件/目录,其中一些有空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5628971/
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-09 08:16:16  来源:igfitidea点击:

Using xcopy to copy multiple files/directories, some of which have spaces

windowscmdxcopy

提问by M Thomas

I'm trying to use xcopy to copy over several files and directories onto an external hard drive. The following command works fine...

我正在尝试使用 xcopy 将多个文件和目录复制到外部硬盘驱动器上。以下命令工作正常...

xcopy d:\location\folder /e 

... except it's not copying over any files/directories withing d:/location/folder that have spaces. I understand that Windows requires file names with spaces need to be enclosed in quotes, but what do I do if I'm trying to do a huge recursive copy where there may be several files or folders with spaces in the name?

...除了它不会复制带有空格的 d:/location/folder 的任何文件/目录。我知道 Windows 需要将带有空格的文件名用引号括起来,但是如果我尝试进行一个巨大的递归复制,其中可能有多个文件或文件夹的名称中带有空格,我该怎么办?

采纳答案by Ken White

Use quotes:

使用引号:

xcopy "d:\location\folder" /e 

回答by Juan González

You should use quotes the following way:

您应该按以下方式使用引号:

xcopy "d:location\folder\anotherfolder\folder with spaces"

Note the unit letter and the colon go outside the quotes and no \at the beginning either.

请注意,单位字母和冒号在引号之外\,开头也没有。

回答by hmadrigal

This may not be the answer to your questions, but an alternative to xcopy is robocopy. See the following xcopy about subdirectories.

这可能不是您问题的答案,但 xcopy 的替代方案是 robocopy。有关子目录,请参阅以下 xcopy。

http://ss64.com/nt/xcopy.html

http://ss64.com/nt/xcopy.html

Additionally if there are spaces in the source or target directories. Then you should wrap them in quotes ("). For example:

此外,如果源目录或目标目录中有空格。然后你应该把它们用引号 (") 括起来。例如:

xcopy "soure path with spaces" "targetPath"

xcopy "带空格的源路径" "targetPath"

Cheers

干杯

回答by Bill IV

The example given makes me uneasy. Maybe needlessly, but my read on the Win cmd.exe window (using [] to enclose related bits :^) is: [command] [d:\location\folder] [DESTINATION DRIVE:LOCATION:FOLDER] [/E]

给出的例子让我感到不安。也许是不必要的,但我在 Win cmd.exe 窗口(使用 [] 来包含相关位 :^)的阅读是: [command] [d:\location\folder] [DESTINATION DRIVE:LOCATION:FOLDER] [/E]

Maybe lower-case /e, works as well, maybe in implied destination to present location works. When I type "help xcopy" is lists the command name, the source, and the destination and the /Upper Case Switches. Yes, destination is optional. But when results don't match your expectation, dropping back to the canonical form, with every jot and tittle in place, seems like a stronger start. One can focus on the thing that's not working.

也许小写的 /e 也可以工作,也许在隐含的目的地现在位置工作。当我键入“help xcopy”时,会列出命令名称、源和目标以及 / 大写开关。是的,目的地是可选的。但是,当结果不符合您的期望时,回到规范形式,每一个细节都到位,似乎是一个更强大的开始。人们可以专注于不起作用的事情。

The help example doesn't use it, but I also feel a bit safer if its clear that the literal path given is the stem and explicitly put in a wildcard:

帮助示例没有使用它,但如果明确给出的文字路径是词干并明确放入通配符,我也觉得有点安全:

C:>xcopy /E C:\Backup* C:\ToyVmBackup
C:\Backup\Backup_VM1_2015-08-27T221110.vbk
C:\Backup\Backup_VM2_2015-08-31T221227.vbk
C:\Backup\VeeamConfigBackup\SVC-L7-WABBOTT\SVC-L7-WABBOTT_2015-08-24_10-00-15.bco
3 File(s) copied

C:>xcopy /EC:\Backup* C:\ToyVmBackup
C:\Backup\Backup_VM1_2015-08-27T221110.vbk
C:\Backup\Backup_VM2_2015-08-31T221227.vbk
C:\ConfigBackup\SABBamLOTT \SVC-L7-WABBOTT_2015-08-24_10-00-15.bco
3 已复制文件

Ok, I prefixed the /E and got away with it. Its ambiguity I'm trying to control, as long as /E starts with a '/' it won't be taken for a path...

好的,我在 /E 前面加上了前缀并逃脱了它。我试图控制它的歧义,只要 /E 以 '/' 开头,它就不会被当作路径......