Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Errorlevel of command executed by batch for loop

The following code always displays 0 as the errorlevel, but when the copy command is done outside of the for loop command it returns a non zero errorlevel.

for /f "usebackq delims=" %%x in (`copy x y`) do (
    set VAR=%%x
)
ECHO Errorlevel = %ERRORLEVEL%
ECHO VAR = %VAR%

Is is possible to get the errorlevel of the copy command executed by the for loop?

like image 915
Josh Avatar asked Jun 21 '10 21:06

Josh


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 )

What does Errorlevel mean?

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.

How do I iterate through a batch file?

In Batch Script, the variable declaration is done with the %% at the beginning of the variable name. The IN list contains of 3 values. The lowerlimit, the increment, and the upperlimit. So, the loop would start with the lowerlimit and move to the upperlimit value, iterating each time by the Increment value.


1 Answers

it works for me ! You only need to put the error checking within the DO parentheses with a text file containing the copy commands (7200 lines; for example: copy 2_97691_Scan.pdf O:\Data\Dev\Mins\PDFScan2\2011\4\2_97691_Scan.pdf), I can run the following batch file

@echo off

setlocal EnableDelayedExpansion

for /F "delims=" %%I in (CopyCurrentPDFs.txt) do (
%%I
if !errorlevel! NEQ 0 echo %%I>>errorcopy.txt
)
like image 151
Gepi Avatar answered Sep 28 '22 09:09

Gepi