的Windows拷贝命令返回codeS? [英] Windows copy command return codes?

查看:182
本文介绍了的Windows拷贝命令返回codeS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 复制X Y
如果%ERRORLEVEL%。 EQ 1.(
    回声复制X Y失败,由于...
    出口/ B
)其他(
  如果%ERRORLEVEL%EQ 2(
      回声复制X Y失败,由于...
      出口/ B
   )
...等...


解决方案

我想在这种情况下,选择 XCOPY 由于错误级别都记录(见的 XCOPY文档,下面转述):

 退出
code说明
==== ===========
  0文件没有错误被复制。
  1没有找到要复制的文件。
  2,用户pressed CTRL + C终止XCOPY。
  发生4初始化错误。那没有
      足够的内存或磁盘空间,或者您输入
      无效的驱动器名称或无效的语法上
      命令行。
  发生5磁盘写入错误。

在任何情况下, XCOPY 是一个更为强大的解决方案。为复制借助相当于文档不记录错误的水平。


顺便说一句,你可能要重新考虑你的使用%ERRORLEVEL%变量。的那有讨厌的后果(至少在某些Windows版本),如果有人已经明确像一些愚蠢的:

 设置错误级别= 22

在这些情况下,实际的可变的将被使用,而不是grebbing实际误差级别。这样做的正常的方式是(因为在错误级别递减的顺序是大于或等于检查):

 如果错误级别2(
    回声复制X Y失败,由于原因2
    出口/ B)
如果错误级别1(
    回声复制X Y失败,由于原因1
    出口/ B


如果您运行的是Win7的或Win Server 2008或更高,你应该考虑的的Robocopy ,也就是现在的preferred大规模复制的解决方案。

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%. eq 1. (
    echo Copy x y failed due to ...
    exit /B
) else (
  if %errorlevel% eq 2. (
      echo Copy x y failed due to ...
      exit /B
   )
... etc ...
)

解决方案

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. That has nasty ramifications (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 grebbing 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
)


If you are running Win7 or Win Server 2008 or later, you should look into Robocopy, which is now the preferred mass-copy solution..

这篇关于的Windows拷贝命令返回codeS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆