gpt4 book ai didi

windows - "invalid path 0 files copied"使用 xcopy 命令时出错

转载 作者:可可西里 更新时间:2023-11-01 12:17:55 24 4
gpt4 key购买 nike

您好,我有一个批量复制文件的小命令,这很有帮助,因为我每天多次执行此特定复制。使用 xcopy 命令时出现问题。一切正常,但我收到此错误:“复制的路径 0 文件无效”。这是代码:

C:\Windows\System32\xcopy  /Y "C:\Users\Ryan\Desktop\mmars_pub\" "C:\Users\Ryan\Desktop\Dropbox\MMARS\mmars_pub\"

我正在使用 xcopy 可执行文件的完整路径,因为我在配置路径环境变量以正常运行时遇到了问题。我想它不应该影响结果。我在某处读到“防止基于 MS-DOS 的程序检测 Windows”复选框应该可以解决问题,但我似乎无法找到它。任何帮助表示赞赏。

最佳答案

原始答案

从源文件夹路径中删除结尾的反斜杠

C:\Windows\System32\xcopy.exe  /Y "C:\Users\Ryan\Desktop\mmars_pub" "C:\Users\Ryan\Desktop\Dropbox\MMARS\mmars_pub\"

编辑 2015/10/01

虽然原始问题使用了文字路径,并且指定的解决方案将解决问题,但还有另一种选择。对于文字路径以及路径在变量内部并且可以(或不)以反斜杠结尾的情况,确保结尾的反斜杠(如果存在)与引号分开就足够了,包括结尾的点。

xcopy /y "x:\source\." "x:\target"
xcopy /y "%myVariable%." "x:\target"

这个结束点不会干扰文件/文件夹名称。如果有和结束反斜杠,额外的点将简单地引用同一个文件夹。如果没有结束反斜杠,如 Windows 文件和文件夹不能以点结束其名称,它将被丢弃。

但是如果要处理 xcopy 命令的输出,请记住这个额外的点将包含在显示的路径中。


注意:解决方案在线上方。如果对出现问题的原因/位置感兴趣,请继续阅读。

为什么 xcopy "c:\source\""d:\target\" 失败但 xcopy "c:\source""d:\target\" 有效?

这两个命令似乎都有有效的路径引用,而且……是的!两者都是有效的路径引用,但有两个元素共同作用导致命令失败:

  • 引用文件夹(注意:它应该被引用,引用路径是一个好习惯,因为您永远不知道它们什么时候会包含空格或特殊字符)
  • xcopy 不是cmd 处理的内部命令,而是一个可执行文件

由于 xcopy 是一个外部命令,它的参数不会按照 cmd 解析器命令行逻辑进行处理。它们由 Microsoft C startup code 处理.

这个解析器遵循两套规则,官方规则

  • 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 quotedstring can be embedded in an argument. Note that the caret (^) is notrecognized 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 everypair of backslashes (\\), and the double quotation mark (") isinterpreted 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 everypair of backslashes (\\) and the double quotation mark is interpretedas an escape sequence by the remaining backslash, causing a literaldouble quotation mark (") to be placed in argv.

和未记录/非官方规则(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 thedouble quoted block continues.

此解析器将在 "first" 参数末尾找到的序列 \" 视为不结束/关闭参数的转义引号,它被视为作为一部分或参数。“第二个”参数的“开始”引号只是结束双引号 block 但没有结束参数,请记住参数是定界的通过空白。

所以虽然它似乎命令行参数是

     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\

xcopy 处理的实际参数是

     v                        v .....argument delimiters
v......................v .....quoted block
xcopy "x:\souce\" "x:\target\"
^.....................^ .....argument data
arg #1

arg #1 = x:\source" x:\target"

当删除结束反斜杠或包含额外的点时,参数中的结束引号将不会被转义,它将关闭引用 block 并且参数之间的空格将被视为分隔符。

关于windows - "invalid path 0 files copied"使用 xcopy 命令时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25840861/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com