Can't get errorlevel from failed MOVE command in batch script
Asked Answered
A

3

8

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

Antilogy answered 3/10, 2013 at 20:20 Comment(0)
S
9

The strange thing about the IF ERRORLEVEL statement is that it doesn't act like you expect- it returns TRUE if the errorlevel is equal to OR GREATER THAN the number specified. A failure in MOVE sets errorlevel to 1 (I just checked) which is greater than 0. Therefore the first clause in the IF statement will always be used. The easiest way to fix your script is to reverse the conditions in the IF statement:

if ERRORLEVEL 1 (echo file was not archived) else (echo file was successfully archived)
Salty answered 3/10, 2013 at 20:56 Comment(3)
Thank you for your answers. Superbob's answer did solve my problem, thank you very much. It is a counterintuitive behaviour indeed. However, the ECHO.%errorlevel% command in my sample still returns 0 so it isn't set to 1 in my case. As for Endoro's answer, can you be more specific? I tried to use %ERRORLEVEL%==0, since no other option would work with errorlevel as a variable, but no result here either.Antilogy
Do you have any statements between the IF ERRORLEVEL 1 and the ECHO.%errorlevel%? If so, they could be resetting the errorlevel. I just tried if ERRORLEVEL 1 (echo ErrorLevel %ERRORLEVEL%, file not archived) else (echo ErrorLevel %ERRORLEVEL%, file successfully archived) and got the expected result ErrorLevel 1, file not archivedSalty
No, I don't have anything in between, but you are right, it works. The problem was that i was echoing %ERRORLEVEL% inside of a for loop and i had DelayedExpansion enabled. I had to use the ! and it worked. Thank you for your help!Antilogy
S
4

Just use %ERRORLEVEL% variable instead of ERRORLEVEL function

Straub answered 3/10, 2013 at 21:37 Comment(0)
I
1

If one wants to use the ERRORLEVEL function, Superbob's answer address' this (though I would recommend the form if NOT ERRORLEVEL 1 (echo file was successfully archived) else (echo file was not archived) instead).

But if one wants to use the %ERRORLEVEL% variable method instead, Delayed Expansion needs to be turned on. The OP's code above, with the recommended changes, is below:

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)
)
Indulgent answered 2/5, 2016 at 18:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.