Windows
Based on the post (dos batch iterate through a delimited string), I wrote a script below but not working as expected.
Goal: Given st
This is the way I would do that:
@echo off
set themes=Sun,Granite,Twilight
echo list = "%themes%"
for %%a in ("%themes:,=" "%") do (
echo file name is %%a
)
That is, change Sun,Granite,Twilight
by "Sun" "Granite" "Twilight"
and then process each part enclosed in quotes in a regular (NO /F option) for
command. This method is much simpler than an iterative for /F
loop based on "delims=,"
.
I made a few modifications to your code.
~ in set list=%~1 to remove quotes so quotes don't accumulate
@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
Looks like it needed the "tokens" keyword...
@echo off
set themes=Sun,Granite,Twilight
call :parse "%themes%"
goto :end
:parse
setlocal
set list=%1
for /F "delims=, tokens=1*" %%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 :end
:getLineNumber
setlocal
echo file name is %1
set filename=%1
endlocal
:end
I took Aacini's answer and only slightly modified it to remove the quotes, so that the quotes can be added or removed as it would be in the desired command.
@echo off
set themes=Hot Sun,Hard Granite,Shimmering Bright Twilight
for %%a in ("%themes:,=" "%") do (
echo %%~a
)