Windows 复制命令返回码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8219040/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:57:22  来源:igfitidea点击:

Windows copy command return codes?

windowsbatch-file

提问by Bill Ruppert

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

回答by paxdiablo

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

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.

In any case, xcopyis a far more powerful solution. The equivalent documentationfor copydoes not document the error levels.

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



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:

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

set errorlevel=22

In those cases, the actual variablewill be used rather than grabbing the actual error level. The "normal" way of doing this is (in decreasing order since errorlevelis a "greater than or equal to" check):

在这些情况下,将使用实际变量而不是获取实际错误级别。这样做的“正常”方法是(按降序排列,因为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
)


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.

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

回答by Wilson Waters

It might also be worth pointing out that xcopy doesn't always return the error code you expect.

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

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

例如,当尝试使用通配符复制多个文件但没有要复制的文件时,您期望返回错误代码 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

回答by Hand-E-Food

I believe Copyonly returns 0 for success or 1 for failure.

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

XCopyhas documented return codes:

XCopy有记录的返回代码:

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.

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

回答by Dominique

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

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

回答by Jeremy Dowsett

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)