1

I've spent a while reading through http://ss64.com/nt/for_cmd.html, going through various other questions, and trying a lot of different little command-line variations of the examples, but still haven't gotten a simple for loop task down yet.

Basically, I want to do something where the command is something like

FOR /f %%G in ("1 2 7 16 21 26 688") do(
    echo %%G
)

The output I want to get is

1
2
...
688

But all I get is

1

And then it exits. Through experimenting with various arguments (i.e. have tried "tokens=*", echo %%G echo %%H, "delims= "), nothing gets the desired output. Rather, I get stuff like

1 2 7 ... 688

or

1 %H
1
  • 1
    /f is (basically) for processing files (or command outputs). To process elements (a list), use for without /f Commented Jul 28, 2016 at 6:08

2 Answers 2

1

Try it like this way with a batch file :

@echo off
FOR %%G in (1 2 7 16 21 26 688) do (
    echo %%G
)
pause>nul
Sign up to request clarification or add additional context in comments.

Comments

0

It appears that it's important to express that you want the 7 specific items extracted by calling for tokens=1-7. Token 1 will be %%G, with each successive, stipulated token using %%H, %%I, etc. Wanting to express one line for each token comes by echoing them 1 echo command at a time for each token's variable, either as one per line stacked or parenthetically grouped sequentially with &s: do (echo %%G) & (echo %%H) & (echo %%I)... etc

In total, the following worked for me:

for /f "tokens=1-7" %%G in ("1 2 7 16 21 26 688") do (  
    echo %%G  
    echo %%H  
    echo %%I  
    echo %%J  
    echo %%K  
    echo %%L  
    echo %%M  
    )

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.