I'm trying to make a very simple bat-file.
In a for-loop I call another application which will create a file for me. When that file is generated I want to do this again until the loop has finished. I need to wait for the file to be created before I run the loop again.
I need to use START
and CALL
, because after the file is created I will kill the process. It's not included in the code below, but it's needed.
@echo off
for /L %%i in (1,1,5) do (
SET filename=fooFile_%%i.txt
START /B CMD /C CALL createFile.bat fooFile_%%i.txt
:waitfile
ECHO waitforfile: %filename%
TIMEOUT /T 10 >nul
IF EXIST %filename% GOTO FoundIt
goto waitfile
:FoundIt
ECHO Found file %filename%
)
ECHO All done!
And my createFile.bat
. This is actually another application. But the code below is enough to mock it:
@echo off
set arg1=%1
TIMEOUT /T 10 >nul
copy NUL %arg1%
ECHO Created file
Current output:
waitforfile:
waitforfile fooFile_1.txt
1 file(s) copied.
Created file
Found file fooFile_1.txt
All done!
As you can see from the output I can't get the loop to work with my GOTO's. I've seen these questions:
Batch file goto not working in for loop
(Windows batch) Goto within if block behaves very strangely
It seems as it's a bad idéa to combine loops with goto's. So how can I solve my problem?