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

  • SEARCH
  • Home
  • 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 4065938
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:04:35+00:00 2026-05-20T16:04:35+00:00

I am trying to use batch scripting to create and assign variables dynamically based

  • 0

I am trying to use batch scripting to create and assign variables dynamically based on a space separated list of computer names.

The computer names are assigned to %hosts% (which are pulled from another file in the production script). What I am trying to accomplish is to count the number of hosts that were pulled in (they are separated by spaces), then assign each one to a variable that contains a number relating to the order the hosts are listed.

Here is the code snippet:

@echo off
setlocal EnableDelayedExpansion

SET hosts=10.10.0.0 192.168.0.0 W2K3-Server
SET /a counter=0

FOR %%I IN (%hosts%) DO (
    SET /a counter+=1
)

echo Total= !counter!

FOR /L %%A IN (1,1,!counter!) DO (
    FOR %%I in (%hosts%) DO (
        IF [!host%%A!]==[] SET host%%A=%%I
        echo host%%A= !host%%A!
    )
)

endlocal

The output of this script is:

Total= 3
host1= 10.10.0.0
host1= 10.10.0.0
host1= 10.10.0.0
host2= 10.10.0.0
host2= 10.10.0.0
host2= 10.10.0.0
host3= 10.10.0.0
host3= 10.10.0.0
host3= 10.10.0.0

The results I would like to see is:

Total= 3
host1= 10.10.0.0
host2= 192.168.0.0
host3= W2K3-Server

I know why I am getting the first result. There is no checking to see if a condition is met. In this case, the condition would be setting one of the hosts as a variable. I need to know how to step out of the “FOR %%I in (%hosts%)” section of the loop once a variable has been set.

Is this possible in batch? This piece is not critical to the project I am working on, but it would add value by opening up some new possibilities in other areas of the script.

  • 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-05-20T16:04:36+00:00Added an answer on May 20, 2026 at 4:04 pm

    Like this?

    @echo off
    setlocal EnableDelayedExpansion
    
    SET hosts=10.10.0.0 192.168.0.0 W2K3-Server
    SET /a counter=0
    
    FOR %%I IN (%hosts%) DO (
        SET /a counter+=1
        IF [!host%%I!]==[] SET host!counter!=%%I
    )
    
    echo Total= !counter!
    
    FOR /L %%A IN (1,1,!counter!) DO (
        echo host%%A= !host%%A!
    )
    
    endlocal
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to understand why one would use Spring Batch over a scripting language
I'm trying to use a batch file to list files in a directory such
I'm trying to use a batch file to create another batch file... it's a
I'm trying to read variables from a batch file for later use in the
I am trying to use batch updates in Access VBA. My code works fine
How can I use a batch file to loop through non-integer values? I'm trying
I am trying to write a batch file so I can use TortoiseHg Annotate
I'm trying to use a batch file to confirm a network connection using ping.
I'm using Mongoid for a project and am trying to use the batch insert
I'm trying to use WSH in place of DOS/Batch files. Thanks for the input!

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.