My current line of batch code is:
for /L %%a in (8000,1,8100) do netstat /a /n | find "%%a" | find "LISTENING" || set tmp_freeport=%%a && goto found
The idea is to find a free port that will be used for listening, within the range of 8000-8100.
Currently, I have port 8000 in use, so the script should go to 8001.
After the loop, %tmp_freeport%
is equal to 8001, as it should be, and its value is used later correctly.
The problem is that the loop keeps running regardless. netstat
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.
Can anyone tell me how to break out of a batch FOR loop?
(Alternatively, if there's a better way of finding a free port, please see my somewhat-related question)