Set errorlevel in Windows batch file

后端 未结 4 951
情书的邮戳
情书的邮戳 2021-02-04 04:47

I am writing a batch script that will loop through each line of a text file, (each line containing a filename) check if the file exists and then runs the file and moves it.

4条回答
  •  攒了一身酷
    2021-02-04 05:23

    @ECHO OFF
    SETLOCAL
    DEL output.txt 2>nul
    REM Loop through each line of input.txt
    FOR /F "tokens=1-3 delims=, " %%i IN (.\ready\input.txt) DO (
      ECHO.
      ECHO.
      ECHO.
      ECHO Check %%i exists, set error flag if it doesnt
      if exist .\ready\%%i (set "errorflag=") ELSE (set errorflag=2)
    CALL echo return code is %%errorflag%%
    
      ECHO Run %%i if it exists
      if NOT DEFINED errorflag (
       call .\ready\%%i
       ECHO Move %%i to archive if no error occured
       if errorlevel 1 (SET errorflag=3) ELSE (ECHO copy .\ready\%%i .\archive\%mydate%_%mytime%_%%j_%%k_%%i)
      )
      ECHO Copy line of text to the new output.txt file if an error occured
      if DEFINED errorflag >>output.txt ECHO %%i, %%j, %%k
    )
    GOTO :EOF
    

    Here's a rewritten procedure.

    Note: output.txt is deleted at the start, else the >> would append to any existing file. 2>nul suppresses error messages if the delete fails (eg. file not exist)

    Within a block statement (a parenthesised series of statements), the ENTIRE block is parsed and THEN executed. Any %var% within the block will be replaced by that variable's value AT THE TIME THE BLOCK IS PARSED - before the block is executed.

    Hence, IF (something) else (somethingelse) will be executed using the values of %variables% at the time the IF is encountered.

    Two common ways to overcome this are 1) to use setlocal enabledelayedexpansion and use !var! in place of %var% to access the chnaged value of var or 2) to call a subroutine to perform further processing using the changed values.

    Note therefore the use of CALL ECHO %%var%% which displays the changed value of var. CALL ECHO %%errorlevel%% displays, but sadly then RESETS errorlevel.

    IF DEFINED var is true if var is CURRENTLY defined.

    ERRORLEVEL is a special varable name. It is set by the system, but if set by the user, the user-assigned value overrides the system value.

    IF ERRORLEVEL n is TRUE if errorlevel is n OR GREATER THAN n. IF ERRORLEVEL 0 is therefore always true.

    The syntax SET "var=value" (where value may be empty) is used to ensure that any stray spaces at the end of a line are NOT included in the value assigned.

    The required commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO COPY to COPY to actually copy the files.

    I used the following input.txt:

    seterr1.bat, J1, K1
    seterr5.bat,J2,K2
    seterr0.bat,J3 K3
    seterr5.bat, J4, K4
    notexist.bat, J5, K5
    

    With existing files seterr*.bat which contain

    @ECHO OFF
    EXIT /b 1
    

    (where the 1 in the last line determines the errorlevel returned)

    and received the resultant output:

    Check seterr1.bat exists, set error flag if it doesnt
    return code is 
    Run seterr1.bat if it exists
    Move seterr1.bat to archive if no error occured
    Copy line of text to the new output.txt file if an error occured
    
    Check seterr5.bat exists, set error flag if it doesnt
    return code is 
    Run seterr5.bat if it exists
    Move seterr5.bat to archive if no error occured
    Copy line of text to the new output.txt file if an error occured
    
    Check seterr0.bat exists, set error flag if it doesnt
    return code is 
    Run seterr0.bat if it exists
    Move seterr0.bat to archive if no error occured
    copy .\ready\seterr0.bat .\archive\__J3_K3_seterr0.bat
    Copy line of text to the new output.txt file if an error occured
    
    Check seterr5.bat exists, set error flag if it doesnt
    return code is 
    Run seterr5.bat if it exists
    Move seterr5.bat to archive if no error occured
    Copy line of text to the new output.txt file if an error occured
    
    Check notexist.bat exists, set error flag if it doesnt
    return code is 2
    Run notexist.bat if it exists
    Copy line of text to the new output.txt file if an error occured
    

    Note that the COPY is merely ECHOed as I mentioned earlier.

    and output.txt

    seterr1.bat, J1, K1
    seterr5.bat, J2, K2
    seterr5.bat, J4, K4
    notexist.bat, J5, K5
    

提交回复
热议问题