2

Given the following for loop from a batch file, why is this only printing A? I have also tried with "tokens=1*"

set str=A/B/C/D/E/F
for /F "delims=/" %%S in ("%str%") do (
    echo %%~S
)

1 Answer 1

1

The delims option defines the delimiters to split each line into seperate tokens. The tokens itself can be accesses by succesive parameters.

set str=A/B/C/D/E/F
for /F "tokens=1,2,3,4,5 delims=/" %%S in ("%str%") do (
    echo %%~S %%T %%U %%V %%W
)

This will split each line, but you want to handle A/B/C/D/E/F as seperate lines.

This can be done by injecting linefeeds

set LF=^


REM ** Two empty lines are required
setlocal EnableDelayedExpansion
set "str=A/B/C/D/E/F"
for %%L in ("!LF!") DO set "str=!str:/=%%~L!"
for /F "delims=/" %%S in ("!str!") do (
    echo %%~S
)

The line for %%L in ("!LF!") DO set "str=!str:/=%%~L!" replaces / with linefeeds

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.