Set errorlevel in Windows batch file

后端 未结 4 943
情书的邮戳
情书的邮戳 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:30

    This is designed to execute the %%i item only if it exists and follow through with checking for errors and move or log. if the %%i item doesn't exist then it will do nothing.

    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, execute it if it does
      if exist .\ready\%%i (
        call .\ready\%%i
        ECHO Move %%i to archive if no error occured 
        if not errorlevel 1 (
            copy .\ready\%%i .\archive\%mydate%_%mytime%_%%j_%%k_%%i
          ) else (
            ECHO Copy line of text to the new output.txt file if an error occurred
            >>output.txt %%i, %%j, %%k
          )
      )
    )
    

提交回复
热议问题