在这个批处理脚本中,我正在测试期望 1
的错误级别返回,但它正在回显 0
.
如果没有要复制的文件,文件夹为空,为什么它会返回 0
成功?
我将通过以下xcopy错误级别:
XCOPY应返回以下退出代码:0复制文件时没有错误 . 1没有找到要复制的文件 . 2用户按下CTRL C终止xcopy . 4发生初始化错误 . 内存或磁盘空间不足,或者在命令行中输入了无效的驱动器名称或无效语法 . 5发生磁盘写入错误 .
这是我的批处理脚本:
@ECHO ON
SET ERRORS=0
REM Backup file first and report any errors if unsuccessful
xcopy /Y "C:\channels\filetransfer_process\*" "C:\channels\backup\"
echo %ERRORLEVEL%
REM Error Checking
REM Note The environmental variable ERRORLEVEL contains the return code of
the last executed program or script.
if errorlevel 2 (
SET BODY="not enough memory or disk space"
GOTO :mailerror
)
if errorlevel 1 (
SET BODY="No files were found to copy"
GOTO :mailerror
)
if errorlevel 5 (
SET BODY="Disk write error occurred"
GOTO :mailerror
)
if NOT ["%errorlevel%"]==["0"] pause
REM Before doing the network connection make sure Z drive is free for use
if exist z:\ ( net use z: /delete )
REM proceed with a network connection using Z with error checking
net use Z: \\dcqwdbs034\D$\arrivals /user:sutter-chs\lawson Sutter1
if errorlevel 0 (
goto :move
) else (
SET BODY="net use connection failed"
goto :mailerror
)
REM move file to MSCM server
:move
move /Y "C:\channels\filetransfer_process\*" "Z:\"
if errorlevel 1 (
SET BODY="File not found, could not be moved/renamed or bad parameters"
goto :mailerror
)
REM remove network mapped Z drive
net use z: /delete
REM perform the error notification via BLAT
:mailerror
"D:\Program Files\BLAT\blat.exe" -Install -Server mail1.sutterhealth.org -f
name@domain.org -u name@domain.org -Pw mypasswd
"D:\Program Files\BLAT\blat.exe" -To name@domain.org.org -Subject "File
Transfer Error" -Body %BODY%
:EOF
1 回答
这是重写的批处理文件,以正确处理所有错误条件(希望未完全测试) .
使用选项
/Q
执行 XCOPY 以禁止复制文件的输出 . 但是 XCOPY 仍然输出复制文件的最终摘要信息 . 此输出将重定向到临时文件中以供以后评估 . 但首先是 XCOPY 评估的退出代码 .if errorlevel 2
表示命令/应用程序的退出代码是 greater or equal2
,因此if errorlevel 5
必须是第一个IF条件 .[
和]
对字符串比较没有特殊含义 . 它们只是两个字面字符 . 所以不要在字符串比较中添加它们 .if NOT "%errorlevel%"=="0" pause
绝对够了 .双引号对于命令解释器具有特殊含义,作为标记参数字符串的开始/结束,其中字符应按字面解释,启用延迟扩展时
%
和!
除外 . 但是请注意 IF 总是包含 IF 来比较这两个参数 . 换句话说,if
在比较参数之前不会删除周围的双引号 .ERRORLEVEL
总是将一个整数值指定为字符串,因此可以安全地使用if NOT %ERRORLEVEL% == 0 pause
使字符串比较更快一些,因为只需要比较两个字节(0
的ASCII表示和终止空字节的字符串,即0x30,0x00)而不是四个字节(引用字节,0
的ASCII表示,引用字节和字符串终止空字节,即0x22,0x30,0x22,0x00) .但最好是使用
if not errorlevel 1
,这意味着前一个命令/应用程序的退出代码是 lower than1
. 几乎所有应用程序在成功时退出0
,在错误条件下退出正数0
. 因此,在命令块内工作的if not errorlevel 1
几乎总是比if %ERRORLEVEL% == 0
好,以便成功进行测试 .此批处理文件中使用的带有选项
/F
的命令 FOR 逐行读取指定文件中的行,跳过空行,跳过以分号开头的行,使用默认分隔符空格和水平制表符将每行拆分为子字符串并指定第一个空格/ tab分隔字符串到指定的循环变量I
. 在这种情况下,这应该始终是复制文件的数量 .应用程序 NET 在错误时通常不会以大于0的值退出 . 因此,在将网络共享映射到驱动器号后,请更好地验证驱动器
Z:
是否确实存在 . 并且最好使网络驱动器映射不持久存储在当前用户的Windows注册表中 . 顺便说一句:有问题的代码中发布的域名,用户名和密码有望是假数据 .要了解使用的命令及其工作方式,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面 .
del /?
echo /?
for /?
goto /?
move /?
net /?
net use /?
pause /?
rem /?
set /?
xcopy /?
也可以看看:
Where does GOTO :EOF return to?
Single line with multiple commands using Windows batch file