How to catch “The system cannot find the file” within For /F loop

batchbatch-filebatch-processingwindows-command-prompt

How to catch "The system cannot find the file" within For /F loop?

I would like my code to do the following:

  • 1 – Search for the specified file
  • 2 – If file not found, handle the "The system cannot find the file"
    by sleeping for 10 seconds and repeating the search
  • 3 – If the file is found (it will be added to the directory
    eventually), continue with the script

Current code for search:

FOR /F "tokens=* delims=" %%x IN (D:\batch\logs\mylog_%1.log) DO (
       SET content=%%x & ECHO !content!
)

%1 is my input parameter once called via .bat script

Best Answer

This worked for me:

:LOG_CHECK

IF EXIST D:\batch\logs\mylog_%1.log (
    GOTO START_LOOP
) ELSE (
    TIMEOUT /T 10 /NOBREAK
)
GOTO LOG_CHECK

REM Restart label
:START_LOOP  
REM my code continues with processes