How to store value in a var using for loop

Ms / Ms dos
August 16, 2010 at 07:08:01
Specs: Windows XP
I am trying to create a Batch file in Windows 2003 (or higher). I have a text file with a list of IP Addresses.

I want to run a for loop by taking the values from the Text file one by one and process it in the body of for loop.

Could someone please assist me with this.

Thanks in advance.

See More: How to store value in a var using for loop

August 16, 2010 at 09:47:56
If each Ip address is on a separate line you could use this code below. Also line1, line2, line3 are the variables for each line in your text file. You can add more by simply copying set /p line3=>nul and replacing line3 with line4 or a different variable. Lastly continue is where it goes after the code has finished so you can put the rest of your code there. Hope I helped

call :label<IP TEXT FILE.txt

goto continue

set /p line1=>nul
set /p line2=>nul
set /p line3=>nul
goto :eof


Report •

August 20, 2010 at 05:39:46
Thanks for your help.

However, the scenerio i have to use is- I want to use the variable again in FOR loop (kind of nested FOR loop) hence I want to store the IPs one by one in that variable and then using same variable in the inner FOR loop body to run another command (telent command).

Hope I am clear in explaining the requirement.

I tried searching a lot for this... However no luck... :(

Report •

February 17, 2011 at 08:22:11
How about

for /f %%i in (iplist.txt) do call :telnet_loop %%i

This assumes that your IP addresses are stored in iplist.txt (one per line). The for command will loop through that file and assign the IP to %%i

In your telnet loop you can retrieve the IP using the %1 variable

Report •

Related Solutions

February 17, 2011 at 20:18:39
try this.

@echo off
for /f %%a in (iplist.txt) do (
echo Processing IP %%a
for /f %k in ("%%a") do (
echo this is your innner loop
echo put your telnet commands here

provide more info on what exactly you want in inner loop and we can comeup with something

Subhash Chandra.

Report •

Ask Question