# Solved: Batch file FOR Statement



## rebelmusic (Feb 5, 2005)

Hey folks

Haven't written any batch files in about 5 years and all my coding knowledge has fallen out of my brain and been replaced with episodes of the simpsons.

Anyway, i've hard coded the whole batch file and it works but i think it could be done in a couple of for statements. Basically i need to assign about 100 variables to multiple ip addresses (which i think has to be hard coded) and then run a command over and over again just changing to variables.

So as an example

"
set ip1=10.10.10.10
set ip2=10.10.10.11
set ip2=10.10.10.12
set ip3=10.10.10.13

rem not the actual command im running

toms.exe -spaip %ip1% spbip %ip2% -user admin -pass password

Each run of the command replaces the variable with the next two, so the following is

toms.exe -spaip %ip3% spbip %ip4% -user admin -pass password

and so on and so on...

Rather than writing this out 50 times, does anyone know of a FOR statement that would loop through the variables? BTW FOR statements never made much sense to me so you're dealing with a bit of a noobie!

Thanks in advance!


----------



## Squashman (Apr 4, 2003)

```
@ECHO OFF & SETLOCAL EnableExtensions EnableDelayedExpansion
FOR /L %%A in (10,2,100) do (
	set /a ip2=%%A+1
	echo IP1:10.10.10.%%A IP2:10.10.10.!ip2!
)
```
Output

```
C:\Users\Squash\batch\Increment>Increment.bat
IP1:10.10.10.10 IP2:10.10.10.11
IP1:10.10.10.12 IP2:10.10.10.13
IP1:10.10.10.14 IP2:10.10.10.15
IP1:10.10.10.16 IP2:10.10.10.17
IP1:10.10.10.18 IP2:10.10.10.19
IP1:10.10.10.20 IP2:10.10.10.21
IP1:10.10.10.22 IP2:10.10.10.23
IP1:10.10.10.24 IP2:10.10.10.25
IP1:10.10.10.26 IP2:10.10.10.27
IP1:10.10.10.28 IP2:10.10.10.29
IP1:10.10.10.30 IP2:10.10.10.31
IP1:10.10.10.32 IP2:10.10.10.33
IP1:10.10.10.34 IP2:10.10.10.35
IP1:10.10.10.36 IP2:10.10.10.37
IP1:10.10.10.38 IP2:10.10.10.39
IP1:10.10.10.40 IP2:10.10.10.41
IP1:10.10.10.42 IP2:10.10.10.43
IP1:10.10.10.44 IP2:10.10.10.45
IP1:10.10.10.46 IP2:10.10.10.47
IP1:10.10.10.48 IP2:10.10.10.49
IP1:10.10.10.50 IP2:10.10.10.51
IP1:10.10.10.52 IP2:10.10.10.53
IP1:10.10.10.54 IP2:10.10.10.55
IP1:10.10.10.56 IP2:10.10.10.57
IP1:10.10.10.58 IP2:10.10.10.59
IP1:10.10.10.60 IP2:10.10.10.61
IP1:10.10.10.62 IP2:10.10.10.63
IP1:10.10.10.64 IP2:10.10.10.65
IP1:10.10.10.66 IP2:10.10.10.67
IP1:10.10.10.68 IP2:10.10.10.69
IP1:10.10.10.70 IP2:10.10.10.71
IP1:10.10.10.72 IP2:10.10.10.73
IP1:10.10.10.74 IP2:10.10.10.75
IP1:10.10.10.76 IP2:10.10.10.77
IP1:10.10.10.78 IP2:10.10.10.79
IP1:10.10.10.80 IP2:10.10.10.81
IP1:10.10.10.82 IP2:10.10.10.83
IP1:10.10.10.84 IP2:10.10.10.85
IP1:10.10.10.86 IP2:10.10.10.87
IP1:10.10.10.88 IP2:10.10.10.89
IP1:10.10.10.90 IP2:10.10.10.91
IP1:10.10.10.92 IP2:10.10.10.93
IP1:10.10.10.94 IP2:10.10.10.95
IP1:10.10.10.96 IP2:10.10.10.97
IP1:10.10.10.98 IP2:10.10.10.99
IP1:10.10.10.100 IP2:10.10.10.101
```


----------



## rebelmusic (Feb 5, 2005)

Thanks Squashman.

Unfortunately what i need is the variables to increment, so one statement to increment, eg

toms.exe -spaip %ip1% spbip %ip2% -user admin -pass password

each run of the for loop would increment %ip1% and %ip2% by 1, so the next run would be %ip3% and %ip4%

The actual ip addresses i'll be using are all random and not relative to one another.

i tried adjusting your example to do this but i'm not having much luck!


----------



## Squashman (Apr 4, 2003)

So you are saying you have 100 IP addresses hard coded with SET statements?

Would make it a lot easier if you put those IP addresses in a TEXT file with TWO of them on a line. Then you could use a For Loop to parse the TEXT file.

So if your text file looks like this

```
192.168.1.10 192.168.1.21
10.10.10.1 10.10.10.5
172.0.0.5 172.0.0.6
```
You could use a FOR loop to parse it.

```
@echo off

FOR /F "tokens=1,2" %%A in (ipaddresses.txt) do echo IP1:%%A IP2:%%B
```
Output

```
E:\batch files\IPaddresses>parseIP.bat
IP1:192.168.1.10 IP2:192.168.1.21
IP1:10.10.10.1 IP2:10.10.10.5
IP1:172.0.0.5 IP2:172.0.0.6
```


----------



## rebelmusic (Feb 5, 2005)

Ok that helps a lot!!

Yes i had them hardcoded 

Any idea how i could increment the second variables? ip3 ip4, ip5 ip6 etc?

toms.exe -spaip ip1 spbip ip2 -user admin -pass password

Thanks for all this!


----------



## Squashman (Apr 4, 2003)

You need to forget about how you had the set statements. The variables for your IP addresses will always be %%A and %%B. It will loop through the text file and pull two new IP address from the text file each time through the loop.
So your batch file will look like this.

```
@echo off

FOR /F "tokens=1,2" %%A in (ipaddresses.txt) do toms.exe -spaip %%A spbip %%B -user admin -pass password
```


----------



## rebelmusic (Feb 5, 2005)

Oh you legend!!!!! Works like a charm! Thanks so much


----------

