7

I'm pretty new to this forum so i first want to thank you for providing me with solutions even before i became a member :).

So I have this code:

for %%a in ("%PBpath%") do ( 
move "network location 1 files" "network location 2" >NUL
if ERRORLEVEL 0 (echo Diagram %%~na.pdf was successfuly archived) else ( echo            Diagram %%~na.pdf was not archived )
ECHO.%errorlevel%
          )

The problem is that I can't get the errorlevel different than 0. Even when the files that are to be copied are missing from location, i still get the successfuly archived message echoed. I searched the forum for similar questions, but i couldn't make it work for some reason. Is there something different between the copy and the ping command (the ping command returns the correct exit code in the errorlevel), because i can't get it with either copy or move...

Thanks! Andrew

4

3 回答 3

7

IF ERRORLEVEL 语句的奇怪之处在于它的行为不像您预期​​的那样 - 如果错误级别等于大于指定的数字,它会返回 TRUE。MOVE 中的失败将 errorlevel 设置为 1(我刚刚检查过),它大于 0。因此将始终使用 IF 语句中的第一个子句。修复脚本的最简单方法是反转 IF 语句中的条件:

if ERRORLEVEL 1 (echo file was not archived) else (echo file was successfully archived)
于 2013-10-03T20:56:59.210 回答
3

只需使用%ERRORLEVEL%变量而不是ERRORLEVEL函数

于 2013-10-03T21:37:00.950 回答
0

如果有人想使用该ERRORLEVEL功能,Superbob 的 回答地址是这个(尽管我会推荐该表格if NOT ERRORLEVEL 1 (echo file was successfully archived) else (echo file was not archived))。

但如果想改用 %ERRORLEVEL% 变量方法,则Delayed Expansion需要打开。上面的 OP 代码以及建议的更改如下:

setlocal enabledelayedexpansion

for %%a in ("%PBpath%") do (
  move "network location 1 files" "network location 2" >NUL
  if !ERRORLEVEL! equ 0 (
    echo Diagram %%~na.pdf was successfully archived
  ) else (
    echo Diagram %%~na.pdf was not archived)
)
于 2016-05-02T18:31:53.357 回答