title: Breaking out of FOR in batch
body: <p>My current line of batch code is:</p>
  
  <pre><code>for /L %%a in (8000,1,8100) do netstat /a /n | find "%%a" | find "LISTENING" || set tmp_freeport=%%a &amp;&amp; goto found
  </code></pre>
  
  <p>The idea is to find a free port that will be used for listening, within the range of 8000-8100.</p>
  
  <p>Currently, I have port 8000 in use, so the script should go to 8001.</p>
  
  <p>After the loop, <code>%tmp_freeport%</code> is equal to 8001, as it should be, and its value is used later correctly.</p>
  
  <p>The problem is that the loop keeps running regardless. <code>netstat</code> is called to search for all 101 ports in the range, which is obviously inefficent and unwanted, because the search must complete before the script can continue.</p>
  
  <p>Can anyone tell me how to break out of a batch FOR loop?</p>
  
  <p>(Alternatively, if there's a better way of finding a free port, please see my <a href="https://stackoverflow.com/questions/9874190/find-avaiable-port-for-php-server">somewhat-related question</a>)</p>