1

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

kasperd
  • 30,455
  • 17
  • 76
  • 124
  • :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 – Mr. Elliott Jan 29 '16 at 11:59

1 Answers1

0

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
sebix
  • 4,313
  • 2
  • 29
  • 47