windows “复制的路径 0 文件无效”使用 xcopy 命令时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25840861/
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
"invalid path 0 files copied" Error while using xcopy command
提问by frontin
Hi I have this little command to copy files in a batch, which will help because I do this specific copy multiple times a day. The problem occurs while using the xcopy command. Everything is in order, but I am receiving this error: "Invalid path 0 files copied". Here is the code:
嗨,我有这个小命令可以批量复制文件,这将有所帮助,因为我每天多次执行此特定复制。使用 xcopy 命令时出现问题。一切正常,但我收到此错误:“复制的路径 0 文件无效”。这是代码:
C:\Windows\System32\xcopy /Y "C:\Users\Ryan\Desktop\mmars_pub\" "C:\Users\Ryan\Desktop\Dropbox\MMARS\mmars_pub\"
I'm using the full path to the xcopy executable because I was having trouble configuring the path environment variable to function properly. I would imagine that it shouldn't affect the result though. I read somewhere about the "Prevent MS-DOS-based programs from detecting Windows" checkbox that should fix the issue, but I just can't seem to find that. Any help appreciated.
我正在使用 xcopy 可执行文件的完整路径,因为我在配置路径环境变量以使其正常运行时遇到问题。我认为它不应该影响结果。我在某处阅读了有关“防止基于 MS-DOS 的程序检测 Windows”复选框的内容,该复选框应该可以解决该问题,但我似乎无法找到它。任何帮助表示赞赏。
回答by MC ND
Original answer
原答案
Remove the ending backslash from the source folder path
从源文件夹路径中删除结尾反斜杠
C:\Windows\System32\xcopy.exe /Y "C:\Users\Ryan\Desktop\mmars_pub" "C:\Users\Ryan\Desktop\Dropbox\MMARS\mmars_pub\"
edited2015/10/01
编辑2015/10/01
While the original question used a literal path, and the indicated solution will solve the problem, there is another option. For literal paths and in cases where the path is inside a variable and could (or not) end in a backslash, it is enough to ensure that the ending backslash (if present) is separated from the quote, including an ending dot.
虽然原始问题使用了文字路径,并且指示的解决方案将解决问题,但还有另一种选择。对于文字路径以及路径在变量内部并且可以(或不)以反斜杠结尾的情况,确保结尾反斜杠(如果存在)与引号(包括结尾点)分开就足够了。
xcopy /y "x:\source\." "x:\target"
xcopy /y "%myVariable%." "x:\target"
This ending dot will not interfere in files/folders names. If there is and ending backslash, the additional dot will simply refer to the same folder. If there is not ending backslash as in windows files and folders can not end their names with a dot, it will be discarded.
此结束点不会干扰文件/文件夹名称。如果有并结束反斜杠,附加的点将只是指同一个文件夹。如果没有结尾反斜杠,如 Windows 文件和文件夹不能以点结尾,它将被丢弃。
BUT if the output of the xcopy
command will be processed, remember that this additional dot will be included in the paths shown.
但是如果xcopy
命令的输出将被处理,请记住这个额外的点将包含在显示的路径中。
note: The solutions are above the line. Keep reading if interested on why/where there is a problem.
注意:解决方案是在线的。如果对为什么/哪里有问题感兴趣,请继续阅读。
Why xcopy "c:\source\" "d:\target\"
fails but xcopy "c:\source" "d:\target\"
works?
为什么xcopy "c:\source\" "d:\target\"
失败但xcopy "c:\source" "d:\target\"
有效?
Both commands seems to have valid path references, and ... YES! both are valid path references, but there are two elements that work together to make the command fail:
这两个命令似乎都有有效的路径引用,并且...是的!两者都是有效的路径引用,但有两个元素共同作用使命令失败:
- the folder reference is quoted (note: it shouldbe quoted, is a good habit to quote paths as you never know when they will contain spaces or special characters)
xcopy
is not an internal command handled bycmd
but an executable file
- 文件夹引用被引用(注意:它应该被引用,引用路径是一个好习惯,因为你永远不知道它们什么时候会包含空格或特殊字符)
xcopy
不是处理的内部命令cmd
而是可执行文件
As xcopy
is an external command, its arguments are not handled following the cmd
parser command line logic. They are handled by the Microsoft C startup code.
与xcopy
外部命令一样,它的参数不会按照cmd
解析器命令行逻辑进行处理。它们由Microsoft C 启动代码处理。
This parser follows two sets of rules, official rules
这个解析器遵循两套规则,官方规则
Arguments are delimited by white space, which is either a space or a tab.
A string surrounded by double quotation marks is interpreted as a single argument, regardless of white space contained within. A quoted string can be embedded in an argument. Note that the caret (
^
) is not recognized as an escape character or delimiter.A double quotation mark preceded by a backslash,
\"
, is interpreted as a literal double quotation mark ("
).Backslashes are interpreted literally, unless they immediately precede a double quotation mark.
If an even number of backslashes is followed by a double quotation mark, then one backslash (
\
) is placed in the argv array for every pair of backslashes (\\
), and the double quotation mark ("
) is interpreted as a string delimiter.If an odd number of backslashes is followed by a double quotation mark, then one backslash (
\
) is placed in the argv array for every pair of backslashes (\\
) and the double quotation mark is interpreted as an escape sequence by the remaining backslash, causing a literal double quotation mark ("
) to be placed in argv.
参数由空格分隔,空格或制表符。
被双引号包围的字符串被解释为单个参数,而不管其中包含的空格。带引号的字符串可以嵌入到参数中。请注意,插入符号 (
^
) 不被识别为转义字符或分隔符。以反斜杠 , 开头的双引号
\"
被解释为文字双引号 ("
)。反斜杠按字面解释,除非它们紧跟在双引号之前。
如果偶数个反斜杠后跟一个双引号,则
\
在 argv 数组中为每对反斜杠 (\\
)放置一个反斜杠 ( ),并且双引号 ("
) 被解释为字符串分隔符。如果奇数个反斜杠后跟一个双引号,则
\
在 argv 数组中为每一对反斜杠 (\\
)放置一个反斜杠 ( ) 并且双引号被剩余的反斜杠解释为转义序列,从而导致"
要放置在 argv 中的文字双引号 ( )。
and undocumented/non official rules (How Command Line Parameters Are Parsed)
和未记录/非官方规则(如何解析命令行参数)
- Outside a double quoted block a
"
starts a double quoted block.- Inside a double quoted block a
"
followed by a different character (not another"
) ends the double quoted block.- Inside a double quoted block a
"
followed immediately by another"
(i.e.""
) causes a single"
to be added to the output, and the double quoted block continues.
- 在双引号块之外,a
"
开始一个双引号块。- 在双引号块中 a
"
后跟一个不同的字符(不是另一个"
)结束双引号块。- 在双引号块中 a
"
紧跟另一个"
(即""
)导致将单个"
添加到输出中,并且双引号块继续。
This parser sees the sequence \"
found at the end of the "first"argument as a escaped quote that does not end/closes the argument, it is seen as part or the argument. And the "starting"quote of the "second"argument is just ending the double quoted block BUT not ending the argument, remember that arguments are delimited by white space.
此解析器将在“第一个”参数\"
末尾找到的序列视为不结束/关闭参数的转义引号,它被视为部分或参数。并且“第二个”参数的“起始”引号只是结束双引号块但不结束参数,请记住参数由空格分隔。
So while it seemsthat the command line arguments are
所以虽然命令行参数似乎是
v v v......argument delimiters
v.........v v..........v ......quoted blocks
xcopy "x:\souce\" "x:\target\"
^.......^ ^........^ ......argument data
arg #1 arg #2
arg #1 = x:\source\
arg #2 = x:\target\
the actual argument handled by xcopy
is
处理的实际参数xcopy
是
v v .....argument delimiters
v......................v .....quoted block
xcopy "x:\souce\" "x:\target\"
^.....................^ .....argument data
arg #1
arg #1 = x:\source" x:\target"
When the ending backslash is removed or the additional dot included, the closing quote in the argument will not be escaped, it will close the quoted block and the space between arguments will be seen as a delimiter.
当结束反斜杠被移除或包含额外的点时,参数中的结束引号将不会被转义,它将关闭被引用的块,并且参数之间的空格将被视为分隔符。