Windows 复制命令返回代码?

发布于 2024-12-17 10:51:51 字数 267 浏览 0 评论 0原文

我想测试批处理文件中副本的成功/失败,但我找不到任何有关返回任何错误级别代码的文档。例如

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

悲歌长辞 2024-12-24 10:51:51

在这种情况下,我会选择 xcopy,因为错误级别已记录(请参阅 xcopy 文档,解释如下):

Exit code  Description
=========  ===========
    0      Files were copied without error.
    1      No files were found to copy.
    2      The user pressed CTRL+C to terminate xcopy.
    4      Initialization error occurred. There is not
           enough memory or disk space, or you entered
           an invalid drive name or invalid syntax on
           the command line.
    5      Disk write error occurred.

无论如何,xcopy 是一个更强大的解决方案。 copy等效文档不记录错误级别。


顺便说一句,您可能需要重新考虑 %errorlevel% 变量的使用。如果有人明确执行了此操作,它会产生意想不到的结果,至少在某些版本的 Windows 中是如此类似这样愚蠢的事情:

set errorlevel=22

在这些情况下,将使用实际的变量,而不是获取实际的错误级别。执行此操作的“正常”方法是(按降序排列,因为 errorlevel 是“大于或等于”检查):

if errorlevel 2 (
    echo Copy x y failed due to reason 2
    exit /B

)
if errorlevel 1 (
    echo Copy x y failed due to reason 1
    exit /B
)

此外,如果您运行的是 Win7 或 Win Server 2008 或更高版本,则应该查看 Robocopy,现在是首选的批量复制解决方案。

I'd opt for xcopy in this case since the error levels are documented (see xcopy documentation, paraphrased below):

Exit code  Description
=========  ===========
    0      Files were copied without error.
    1      No files were found to copy.
    2      The user pressed CTRL+C to terminate xcopy.
    4      Initialization error occurred. There is not
           enough memory or disk space, or you entered
           an invalid drive name or invalid syntax on
           the command line.
    5      Disk write error occurred.

In any case, xcopy is a far more powerful solution. The equivalent documentation for copy 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:

set errorlevel=22

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):

if errorlevel 2 (
    echo Copy x y failed due to reason 2
    exit /B

)
if errorlevel 1 (
    echo Copy x y failed due to reason 1
    exit /B
)

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.

知足的幸福 2024-12-24 10:51:51

还值得指出的是,xcopy 并不总是返回您期望的错误代码。

例如,当尝试使用通配符复制多个文件但没有要复制的文件时,您期望返回错误代码为 1(“未找到要复制的文件”),但实际上返回 0(“复制文件时没有错误”) )

C:\Users\wilson>mkdir bla

C:\Users\wilson>mkdir blert

C:\Users\wilson>xcopy bla\* blert\
0 File(s) copied

C:\Users\wilson>echo %ERRORLEVEL%
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")

C:\Users\wilson>mkdir bla

C:\Users\wilson>mkdir blert

C:\Users\wilson>xcopy bla\* blert\
0 File(s) copied

C:\Users\wilson>echo %ERRORLEVEL%
0
ゝ杯具 2024-12-24 10:51:51

我相信 Copy 仅返回 0 表示成功或 1 表示失败。

XCopy 已记录返回代码:

0 = 文件复制成功,没有错误。
1 = 未找到要复制的文件。
2 = 用户按 CTRL+C 终止 xcopy。
4 = 发生初始化错误。内存或磁盘空间不足,或者您在命令行中输入了无效的驱动器名称或无效语法。
5 = 发生磁盘写入错误。

I believe Copy only returns 0 for success or 1 for failure.

XCopy has documented return codes:

0 = Files were copied without error.
1 = No files were found to copy.
2 = The user pressed CTRL+C to terminate xcopy.
4 = Initialization error occurred. There is not enough memory or disk space, or you entered an invalid drive name or invalid syntax on the command line.
5 = Disk write error occurred.

难理解 2024-12-24 10:51:51

还有一点我想强调一下:xcopy以及robocopy只能复制文件,但不能重命名。
在查看原始情况时(copy x y,对我来说这看起来像是重命名),我的印象是 copy 命令仍然是唯一适合此目的的命令。

There is also one point I would like to emphasize: xcopy as well as robocopy 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 the copy command still is the only one suitable for this purpose.

奈何桥上唱咆哮 2024-12-24 10:51:51
Error# Description 
0 No error 
1 Not owner 
2 No such file or directory 
3 Interrupted system call 
4 I/O error 
5 Bad file number 
6 No more processes 
7 Not enough core memory 
8 Permission denied 
9 Bad address 
10 File exists 
11 No such device 
12 Not a directory 
13 Is a directory 
14 File table overflow 
15 Too many open files 
16 File too large 
17 No space left on device 
18 Directory not empty 
999 Unmapped error (ABL default) 

Error# Description 
0 No error 
1 Not owner 
2 No such file or directory 
3 Interrupted system call 
4 I/O error 
5 Bad file number 
6 No more processes 
7 Not enough core memory 
8 Permission denied 
9 Bad address 
10 File exists 
11 No such device 
12 Not a directory 
13 Is a directory 
14 File table overflow 
15 Too many open files 
16 File too large 
17 No space left on device 
18 Directory not empty 
999 Unmapped error (ABL default) 

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文