Windows 复制命令返回代码?
我想测试批处理文件中副本的成功/失败,但我找不到任何有关返回任何错误级别代码的文档。例如
copy x y
if %errorlevel%. equ 1. (
echo Copy x y failed due to ...
exit /B
) else (
if %errorlevel% equ 2. (
echo Copy x y failed due to ...
exit /B
)
... etc ...
)
I would like to test for the success/failure of a copy in a batch file, but I can't find any documentation on what if any errorlevel codes are returned. For example
copy x y
if %errorlevel%. equ 1. (
echo Copy x y failed due to ...
exit /B
) else (
if %errorlevel% equ 2. (
echo Copy x y failed due to ...
exit /B
)
... etc ...
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在这种情况下,我会选择 xcopy,因为错误级别已记录(请参阅 xcopy 文档,解释如下):
无论如何,
xcopy
是一个更强大的解决方案。copy
的等效文档不记录错误级别。顺便说一句,您可能需要重新考虑
%errorlevel%
变量的使用。如果有人明确执行了此操作,它会产生意想不到的结果,至少在某些版本的 Windows 中是如此类似这样愚蠢的事情:在这些情况下,将使用实际的变量,而不是获取实际的错误级别。执行此操作的“正常”方法是(按降序排列,因为
errorlevel
是“大于或等于”检查):此外,如果您运行的是 Win7 或 Win Server 2008 或更高版本,则应该查看 Robocopy,现在是首选的批量复制解决方案。
I'd opt for
xcopy
in this case since the error levels are documented (see xcopy documentation, paraphrased below):In any case,
xcopy
is a far more powerful solution. The equivalent documentation forcopy
does not document the error levels.As an aside, you may want to rethink your use of the
%errorlevel%
variable. It has unexpected results, at least in some versions of Windows, if someone has explicitly done something silly like:In those cases, the actual variable will be used rather than grabbing the actual error level. The "normal" way of doing this is (in decreasing order since
errorlevel
is a "greater than or equal to" check):In addition, if you are running Win7 or Win Server 2008 or later, you should look into Robocopy, which is now the preferred mass-copy solution.
还值得指出的是,xcopy 并不总是返回您期望的错误代码。
例如,当尝试使用通配符复制多个文件但没有要复制的文件时,您期望返回错误代码为 1(“未找到要复制的文件”),但实际上返回 0(“复制文件时没有错误”) )
It might also be worth pointing out that xcopy doesn't always return the error code you expect.
For example when trying to copy multiple files with a wildcard but there are no files to copy you expect a return error code of 1 ("No files were found to copy"), but it actually returns 0 ("Files were copied without error")
我相信
Copy
仅返回 0 表示成功或 1 表示失败。XCopy
已记录返回代码:I believe
Copy
only returns 0 for success or 1 for failure.XCopy
has documented return codes:还有一点我想强调一下:
xcopy
以及robocopy
只能复制文件,但不能重命名。在查看原始情况时(
copy x y
,对我来说这看起来像是重命名),我的印象是copy
命令仍然是唯一适合此目的的命令。There is also one point I would like to emphasize:
xcopy
as well asrobocopy
can only copy files, but they can't rename them.While looking at the original situation (
copy x y
, which looks like a rename to me), I have the impression that thecopy
command still is the only one suitable for this purpose.