视窗
根据帖子(dos批处理通过分隔字符串),我在下面编写了一个脚本,但没有按预期工作.
目标:给定字符串"Sun,Granite,Twilight",我想在循环中获取每个主题值,以便我可以使用该值进行一些处理.
当前输出不正确:
list = "Sun,Granite,Twilight"
file name is "Sun Granite Twilight"
Run Code Online (Sandbox Code Playgroud)
对于第一次迭代,它应该是:
list = "Sun,Granite,Twilight"
file name is "Sun"
Run Code Online (Sandbox Code Playgroud)
然后第二次迭代应该是"文件名是"花岗岩"等等.我做错了什么?
码:
set themes=Sun,Granite,Twilight
call :parse "%themes%"
goto :end
:parse
setlocal
set list=%1
echo list = %list%
for /F "delims=," %%f in ("%list%") do (
rem if the item exist
if not "%%f" == "" call :getLineNumber %%f
rem if next item exist
if not "%%g" == "" call :parse "%%g"
)
endlocal
:getLineNumber
setlocal
echo file name is %1
set filename=%1
endlocal
:end
Run Code Online (Sandbox Code Playgroud)
Aac*_*ini 44
这就是我这样做的方式:
@echo off
set themes=Sun,Granite,Twilight
echo list = "%themes%"
for %%a in ("%themes:,=" "%") do (
echo file name is %%a
)
Run Code Online (Sandbox Code Playgroud)
也就是说,Sun,Granite,Twilight通过"Sun" "Granite" "Twilight"常规(NO/F选项)for命令更改,然后处理括在引号中的每个部分.这种方法比基于迭代for /F循环的方法简单得多"delims=,".
小智 21
我接受了Aacini的回答,并稍微修改了它以删除引号,以便可以在所需命令中添加或删除引号.
@echo off
set themes=Hot Sun,Hard Granite,Shimmering Bright Twilight
for %%a in ("%themes:,=" "%") do (
echo %%~a
)
Run Code Online (Sandbox Code Playgroud)
我对你的代码做了一些修改.
〜在set list =%~1中删除引号,因此引号不会累积
@echo off
set themes=Sun,Granite,Twilight
call :parse "%themes%"
pause
goto :eof
:parse
setlocal
set list=%~1
echo list = %list%
for /F "tokens=1* delims=," %%f in ("%list%") do (
rem if the item exist
if not "%%f" == "" call :getLineNumber %%f
rem if next item exist
if not "%%g" == "" call :parse "%%g"
)
endlocal
goto :eof
:getLineNumber
setlocal
echo file name is %1
set filename=%1
goto :eof
Run Code Online (Sandbox Code Playgroud)