1

Right now I need to loop through the content of a file,

This loop works fine and prints the content of the file

for /F %%x in (Test.txt) do @echo %%x

However because later on I want to do something else with the content I need to have more than one statement in this loop therefor I would like to use the following syntax:

for /F in (Test.txt) do 
some more statements
done

Unfortunately the latter does not work? How do I get a for loop to execute multiple commands?

3
  • 2
    This syntax is not bash. Looks more like batch to me. Commented Oct 17, 2019 at 8:07
  • Possible duplicate of second command in a batch for loop Commented Oct 17, 2019 at 8:15
  • 2
    @bishop The tagged link is actually not a possible duplicate. It's related to a goto statement inside a codeblock which then exits the loop, where this question is an attempt to run multiple commands for a single for statement. Commented Oct 17, 2019 at 8:31

1 Answer 1

1

To run multiple commands for a single loop, you need to enclose the commands in a parenthesised code block:

for /F %%I in (Test.txt) do (
   echo %%i
   echo "%%i"
   echo etc
)

Be careful however as setting and using variables inside of a loop may require delayedexpansion

See from cmd.exe help for:

for /?
setlocal /?
set /? 
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.