相关疑难解决方法(0)

dos批处理迭代分隔的字符串

我有一个我想单独处理的IP分隔列表.列表长度提前未知.如何拆分和处理列表中的每个项目?

@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
Run Code Online (Sandbox Code Playgroud)

输出:

In subroutine
127.0.0.1
In subroutine
ECHO is off.
Run Code Online (Sandbox Code Playgroud)

更新: 使用Franci的答案作为参考,这是解决方案:

@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 …
Run Code Online (Sandbox Code Playgroud)

command-line batch-file

36
推荐指数
5
解决办法
9万
查看次数

标签 统计

batch-file ×1

command-line ×1