Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 454
user2844129 Avatar asked Oct 03 '13 20:10

user2844129


People also ask

What is Errorlevel in batch file?

Batch file error level: %ERRORLEVEL% is an environment variable that contains the last error level or return code in the batch file – that is, the last error code of the last command executed. Error levels may be checked by using the %ERRORLEVEL% variable as follows: IF %ERRORLEVEL% NEQ 0 ( DO_Something )

How can you end the batch file if the last command failed?

EXIT /B at the end of the batch file will stop execution of a batch file. Use EXIT /B < exitcodes > at the end of the batch file to return custom return codes. Environment variable %ERRORLEVEL% contains the latest errorlevel in the batch file, which is the latest error codes from the last command executed.

What does Errorlevel 2 mean?

For example, the diff program has three exit codes: 0 means the files are the same; 1 means the files are different; 2 means that something terrible happened. There are also programs that use an exit code of zero to mean success and anything else to mean failure.

What does Errorlevel mean?

Updated: 06/06/2021 by Computer Hope. In Microsoft Windows and MS-DOS, an errorlevel is the integer number returned by a child process when it terminates. Errorlevel is 0 if the process was successful. Errorlevel is 1 or greater if the process encountered an error.


2 Answers

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)
like image 169
Superbob Avatar answered Oct 05 '22 17:10

Superbob


Just use %ERRORLEVEL% variable instead of ERRORLEVEL function

like image 24
Endoro Avatar answered Oct 05 '22 19:10

Endoro