DOS批处理通过分隔字符串迭代 [英] dos batch iterate through a delimited string
问题描述
我有IP地址的分隔列表,我想单独处理。该列表的长度是提前不明。我如何分割和处理列表中的每个项目?
关闭@echo
组服务器= 127.0.0.1,192.168.0.1,10.100.0.1FOR / F令牌= * delims =%%一个IN(%服务器%)DO拨打:子%%一:子
Echo在子程序
回音1%
退出/ B
输出:
在子程序
127.0.0.1
在子程序
ECHO处于关闭状态。
更新:
使用弗郎的答案作为参考,这里的解决方案:
关闭@echo
组服务器= 127.0.0.1,192.168.0.1,10.100.0.1拨打:解析%服务器%
转到:结束
:解析
SETLOCAL
集列表=%1
集列表=%列表:=%
FOR / F令牌= 1 * delims =%%一个IN(%目录%)DO(
如果不是%%一个==呼:分%%一
如果不是%% B==电话:解析%% B
)
ENDLOCAL
退出/ B:子
SETLOCAL
Echo在子程序
回音1%
ENDLOCAL
退出/ B:结束
输出:
在子程序
127.0.0.1
在子程序
192.168.0.1
在子程序
10.100.0.1
更新:如果不知道项目在列表中的号码,它仍然是可能的解析与简单而又重复的所有项目该列表的头部。 (我已经改变了IP地址,以简单的数字的名单的简单)
的最后是Lisp语言类我花了18年前还清了... 的
关闭@echo
SETLOCAL组服务器= 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24回声%服务器%拨打:解析%服务器%转到:EOS:解析集列表=%1
集列表=%列表:=%FOR / F令牌= 1 * delims =%%一个IN(%目录%)DO(
如果不是%%一个==呼:分%%一
如果不是%% B==电话:解析%% B
)转到:EOS:子回音1%转到:EOS:EOS
ENDLOCAL
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
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
这篇关于DOS批处理通过分隔字符串迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!