title: Batch file - Wait for file in loop
body: <p>I'm trying to make a very simple bat-file.</p>
  
  <p>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.</p>
  
  <p>I  need to use <code>START</code> and <code>CALL</code>, because after the file is created I will kill the process. It's not included in the code below, but it's needed.</p>
  
  <pre><code>@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 &gt;nul
          IF EXIST %filename% GOTO FoundIt
          goto waitfile
  
      :FoundIt
          ECHO Found file %filename%
  )
  ECHO All done!
  </code></pre>
  
  <p>And my <code>createFile.bat</code>. This is actually another application. But the code below is enough to mock it:</p>
  
  <pre><code>@echo off
  
  set arg1=%1
  TIMEOUT /T 10 &gt;nul
  copy NUL %arg1%
  ECHO Created file
  </code></pre>
  
  <p>Current output:</p>
  
  <pre><code>waitforfile:
  waitforfile fooFile_1.txt
      1 file(s) copied.
  Created file
  Found file fooFile_1.txt
  All done!
  </code></pre>
  
  <p>As you can see from the output I can't get the loop to work with my GOTO's. I've seen these questions:</p>
  
  <p><a href="https://stackoverflow.com/questions/14983972/batch-file-goto-not-working-in-for-loop">Batch file goto not working in for loop</a></p>
  
  <p><a href="https://stackoverflow.com/questions/8481558/windows-batch-goto-within-if-block-behaves-very-strangely">(Windows batch) Goto within if block behaves very strangely</a></p>
  
  <p>It seems as it's a bad idéa to combine loops with goto's. So how can I solve my problem?</p>