xcopy returns 设置排除参数时出现错误 "Invalid number of parameters"

xcopy returns error "Invalid number of parameters" when exclude parameter is set

发行:

xcopy X:\ "Y:\...\bin76543210\" /c /g /d /i /e /r /h /y

按预期工作。然而:

xcopy X:\ "Y:\...\bin76543210\" /c /g /d /i /e /r /h /y /exclude:"Y:\...\exclude.txt"

returns 错误:

Invalid number of parameters

当路径名(包含空格)未用引号引起来时也会发生这种情况。然而,事实并非如此。路径(为便于阅读而编辑)都正确对应。语法(根据 Product Documentation - Xcopy)也是正确的。关于 OS 是 Windows XP Professional x32 SP3。

为什么第二个命令返回错误,如何解决?我不是在寻找 xcopy 的替代品(robocopy 等)。

/EXCLUDE:file 开关不会排除指定的文件。根据 xcopy command reference:

/exclude:FileName1[+[FileName2][+[FileName3](…)] Specifies a list of files. At least one file must be specified. Each file will contain search strings with each string on a separate line in the file. When any of the strings match any part of the absolute path of the file to be copied, that file will be excluded from being copied.

XCOPY 是一个古老的命令,可以追溯到 DOS 时代。 /EXCLUDE 选项似乎从未更新为支持长文件名。呃:-(

如果您删除引号,则 space 之后的文本将被解释为附加参数,并且您会收到 "Invalid number of parameters" 错误。如果保留引号,它会将引号视为路径的一部分,并报告找不到文件。

我相信你有三种可能的解决方案:

1) 在您的路径中使用简短的 8.3 文件夹名称。

当然,如果您的卷禁用了短名称,这将无法工作。

2) 使用 SUBST 命令为您的麻烦路径创建一个驱动器别名。

subst Q: "Y:\path with spaces"
xcopy X:\ "Y:\...\bin76543210\" /c /g /d /i /e /r /h /y /exclude:Q:exclude.txt
subst Q: /d

如果您不知道可用的驱动器盘符,这可能是个问题。

3)(我的最爱)简单地 PUSHD 完成麻烦的路径,然后 运行 从那里的命令 :-)

pushd "Y:\path with spaces"
xcopy X:\ "Y:\...\bin76543210\" /c /g /d /i /e /r /h /y /exclude:exclude.txt
popd



有关详细信息,请参阅 https://sevenx7x.wordpress.com/2009/01/02/xcopy-with-exclude-option-shows-cant-read-file/ and http://forums.majorgeeks.com/showthread.php?t=54300