dos batch iterate through a delimited string

2019-01-16 12:33发布

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 call :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

7条回答
时光不老,我们不散
2楼-- · 2019-01-16 13:04

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 ';'"
查看更多
Evening l夕情丶
3楼-- · 2019-01-16 13:07

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.

查看更多
爷、活的狠高调
4楼-- · 2019-01-16 13:07

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.

查看更多
男人必须洒脱
5楼-- · 2019-01-16 13:08

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.

查看更多
Summer. ? 凉城
6楼-- · 2019-01-16 13:09

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"
查看更多
一夜七次
7楼-- · 2019-01-16 13:21

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.

查看更多
登录 后发表回答