dos batch iterate through a delimited string
Solution 1:
The for command handles a number of delimiters by default. In this case you can do
@echo off
set servers=127.0.0.1,192.168.0.1,10.100.0.1
for %%i in (%servers%) do (
echo %%i
)
If you run into a delimiter that is not natively supported, you could do a replace to first prepare the string so it is in the right format
@echo off
set [email protected]@10.100.0.1
set servers=%servers:@=,%
for %%i in (%servers%) do (
echo %%i
)
Using recursive calls has the chance to run out of stack space once you go over a certain number of items in the list. Also testing it, the recursion pattern seems to run a bit slower.
Solution 2:
Update: If the number of items in the list is not known, it is still possible to parse all items with simple recursion on the head of the list. (I've changed the IPs to simple numbers for simplicity of the list)
Finally that Lisp class I took 18 years ago paid off...
@echo off
setlocal
set servers=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24
echo %servers%
call :parse "%servers%"
goto :eos
:parse
set list=%1
set list=%list:"=%
FOR /f "tokens=1* delims=," %%a IN ("%list%") DO (
if not "%%a" == "" call :sub %%a
if not "%%b" == "" call :parse "%%b"
)
goto :eos
:sub
echo %1
goto :eos
:eos
endlocal
Solution 3:
The difficulty is that the for command is intended to work on multiline text strings that you would typically find in files. The best solution I've found to this problem is to modify your string to be multiline.
rem You need to enable delayed expansion, otherwise the new line will interfere
rem with the for command.
setlocal ENABLEDELAYEDEXPANSION
rem The following three lines escape a new line and put it in a variable for
rem later.
rem The empty lines are important!
set newline=^
set list=jim alf fred
for /F %%i in ("%list: =!newline!%") do (echo item is %%i)
This final for loop will iterate over the items in the space separated variable. It does it by replacing the space in the list variable with newlines, then doing the normal for loop.
Solution 4:
I can't believe this is so complicated! Seems like everyone would want to split a string frequently without knowing how many tokens are in it and without parsing it by lines. Typically, it seems people are writing to a file and then step through it or using a multi sub method like these others. What a mess!
Here's my solution. It's easier to follow and works inline, i.e. no sub routine. It is nice to build a list of filenames or whatever and then step through them without having to write to them to a temp file, ensure there is no file writing collision, delete the temp file etc. Plus I don't know how to return a value from a sub like it was a function - I don't think you can. So you'd have to keep writing 2 subs with the other answers I see here each time you wanted to do something like this - one that feeds the other. My method lets you easily create lists and step through them as many times as you want throughout the script.
setlocal enableextensions enabledelayedexpansion
set SomeList=
set SomeList=!SomeList!a;
set SomeList=!SomeList!b;
set SomeList=!SomeList!c;
set SomeList=!SomeList!d;
set SomeList=!SomeList!e;
set SomeList=!SomeList!f;
set SomeList=!SomeList!g;
set List=!SomeList!
:ProcessList
FOR /f "tokens=1* delims=;" %%a IN ("!List!") DO (
if "%%a" NEQ "" (
echo %%a
)
if "%%b" NEQ "" (
set List=%%b
goto :ProcessList
)
)
Output:
a
b
c
d
e
f
g
Obviously you can just exchange the echo with something useful.
Solution 5:
Yes, I know this is a very old topic, but I recently discovered and interesting trick that can perform the requested split in a much simpler way. Here it is:
set n=1
set "server[!n!]=%servers:,=" & set /A n+=1 & set "server[!n!]=%"
These two lines split the servers
string into the server
array and also define n
variable with the number of created elements. This way, you just need to use a for /L
command from 1 to %n%
to process all elements. Here it is the complete code:
@echo off
setlocal EnableDelayedExpansion
set servers=127.0.0.1,192.168.0.1,10.100.0.1
set n=1
set "server[!n!]=%servers:,=" & set /A n+=1 & set "server[!n!]=%"
for /L %%i in (1,1,%n%) do echo Process server: !server[%%i]!
This method is not just simpler and faster than any other method based on for
command, but it also allows to directly use a multi-character string as delimiter.
You may read further details on the splitting method at this post.