title: If you cannot delete the file, wait and try again
body: <p>The warning:</p>
  <blockquote>
  <p>Warning: unlink(temp/1.pdf): Resource temporarily unavailable in
  C:\www\test.php on line 122</p>
  <p>Warning:
  unlink(temp/2.pdf): Resource temporarily unavailable in
  C:\www\test.php on line 123</p>
  </blockquote>
  <p>I'm using PHP's <code>exec</code> to run a batch file to perform some OCR, so the file cannot be deleted at the time the code is running, as CMD is busy creating the files. Once the files are created, I then use FPDF/FPDI to merge both PDFs, creating a new file in a different directory (merged/file.pdf)</p>
  <p>So, what I need to do is wait for the batch file to finish executing, and for CMD to release the files so they can be deleted.</p>
  <p>I just don't know how to do this.</p>
  <pre><code>$scriptName = 'wscript &quot;launchquiet.vbs&quot; &quot;C:\www\OCR.bat&quot;';
  exec($scriptName,$out);
  foreach($out as $key =&gt; $value) {echo $key.&quot; &quot;.$value.&quot;&lt;br&gt;&quot;;}
  </code></pre>
  <p>That does not work, even though I should be forcing PHP to wait for the script's output by forcing it to wait for the script to finish.</p>
  <p>I've also tried</p>
  <pre><code>exec('wscript &quot;launchquiet.vbs&quot; &quot;C:\www\OCR.bat&quot; &amp;');
  </code></pre>
  <p>And</p>
  <pre><code>exec('wscript &quot;launchquiet.vbs&quot; &quot;C:\www\OCR.bat&quot; 2&gt;&amp;1 &amp;');
  </code></pre>
  <p>With the <code>&amp;</code> and <code>2&gt;&amp;1 &amp;</code>, which should (from Googling) force PHP to wait for the script to finish, but it doesn't.</p>
  <p>So, my question namely becomes: is there a way to do this, forcing PHP to wait until the exec finishes; or, how do I make PHP wait, say, 10 seconds, upon deletion failure to try it again (and then wait again if there's another failure)</p>
  <p>Edit: the code that does the deleting: <code>unlink(&quot;temp/1.pdf&quot;);</code> and <code>unlink(&quot;temp/2.pdf&quot;);</code></p>
  <p>Edit 2: The whole code</p>
  <pre><code>exec('wscript &quot;launchquiet.vbs&quot; &quot;C:\www\OCR.bat&quot;');
      
  PDF::Merge(&quot;temp/&quot; . $newFileName . &quot;.png&quot;, &quot;temp/&quot; . $newFileName . &quot;_1.pdf&quot;, &quot;temp/&quot; . $newFileName . &quot;_2.pdf&quot;, $newFileName);
  unlink(&quot;OCR.bat&quot;); // Delete Files
  unlink(&quot;temp/&quot; . $newFileName . &quot;.png&quot;);
  unlink(&quot;temp/&quot; . $newFileName . &quot;_1.pdf&quot;;
  unlink(&quot;temp/&quot; . $newFileName . &quot;_2.pdf&quot;;
  </code></pre>