I have a delimited list of IPs I'd like to process individually. The list length is unknown ahead of time. How do I split and process each item in the list?
@echo off
set servers=127.0.0.1,192.168.0.1,10.100.0.1
FOR /f "tokens=* delims=," %%a IN ("%servers%") DO cal开发者_如何学JAVAl :sub %%a
:sub
echo In subroutine
echo %1
exit /b
Outputs:
In subroutine
127.0.0.1
In subroutine
ECHO is off.
Update: Using Franci's answer as reference, here's the solution:
@echo off
set servers=127.0.0.1,192.168.0.1,10.100.0.1
call :parse "%servers%"
goto :end
:parse
setlocal
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"
)
endlocal
exit /b
:sub
setlocal
echo In subroutine
echo %1
endlocal
exit /b
:end
Outputs:
In subroutine
127.0.0.1
In subroutine
192.168.0.1
In subroutine
10.100.0.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 servers=127.0.0.1@192.168.0.1@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.
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
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.
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.
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.
This is a generalisation of Franci's solution, so that you can perform any job on each item in your list, without having copies of the list iteration code.
:list_iter rem Usage: call :list_iter :do_sub_for_each_item "CSV of items" set foo=%1 set list=%~2 for /f "tokens=1* delims=," %%i in ("%list%") do ( call %foo% %%i if not "%%j" == "" ( call :list_iter %foo% "%%j" ) ) exit /b
Eg, you can call it by:
call :list_iter :my_foo "one,two,three" call :list_iter :my_bar "sun,poo,wee"
If PowerShell is available you could:
C:>set servers=127.0.0.1,192.168.0.1,10.100.0.1
C:>powershell -NoProfile -Command "('%servers%').Split(',')"
127.0.0.1
192.168.0.1
10.100.0.1
Another use to display a readable PATH variable.
@powershell -NoProfile -Command "($Env:PATH).Split(';')"
or
powershell -NoProfile -Command "$Env:PATH -split ';'"
精彩评论