Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7708539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:38:17+00:00 2026-06-01T00:38:17+00:00

My current line of batch code is: for /L %%a in (8000,1,8100) do netstat

  • 0

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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T00:38:18+00:00Added an answer on June 1, 2026 at 12:38 am

    You are correct that a FOR /L loop “always” counts to completion. (Well, actually there are some drastic coding methods that can break it, but no need to go there)

    However, even though the loop counts to completion, the DO clause is skipped once the GOTO FOUND statement has been executed. You can prove this to yourself by inserting echo testing %%a& in the front of your existing DO clause. You will see that no additional testing takes place once you have found your free port.

    The FOR /L loop counts very fast (at least by batch standards), so I wouldn’t worry about the extra counting. Now if your were counting 1 through 1 million, then I might worry. At 10 million I am definitely concerned.

    I can simplify your existing logic a bit. You can combine the 2 FIND statements into a single FINDSTR using a regular expression.

    for /L %%a in (8000,1,8100) do netstat /a /n | findstr /rc:"%%a.*LISTENING" || set tmp_freeport=%%a && goto found
    

    I am no expert concerning IP addresses and ports, but I am concerned that your FIND logic may be inadequate to screen for used ports. I think looking for a colon before and a space after the port will make it more reliable. Also, I doubt you want to see the text of the used ports, so I redirected the FINDSTR output to nul.

    for /L %%a in (8000,1,8100) do netstat /a /n | findstr /rc:":%%a .*LISTENING" >nul || set tmp_freeport=%%a && goto found
    

    Note: GOTO :Label will immediately terminate all other forms of a FOR loop. Only the FOR /L variant continues to count after a GOTO.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

here is my current line of code: $(<li>).text($(this).text()).appendTo(#theList); Which results in <ul id=theList> <li>blah</li>
I am trying to set the current line number to a variable in Elisp
Line directives (#line) are used to reconfigure perl's idea of the current filename and
VIM can be configured to highlight current line via :hi cursorline guibg=green and set
I highlight current line by evaluating: (hl-line-mode) It could also be set globally: (global-hl-line-mode
there! I want to find out how to change current line format in QTextEdit?
How do I get the current line and column numbers in a RichTextBox in
Just trying to get my irb sessions to actually list the current line of
Is there a way to run a regexp-string replace on the current line in
I want to force the current execution line to a specific line in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.