0

I have a code snippet in a bat file which reads a text file and echos each line like this.

This one works.

FOR /F "tokens=*" %%i IN (tags.txt) DO (
    @ECHO %%i
)

This one does not work. (Echos tags.txt)

set file="tags.txt"

FOR /F "tokens=*" %%i IN (%file%) DO (
    @ECHO %%i
)

What is wrong?

2 Answers 2

2

Try

FOR /F "usebackqtokens=*" %%i IN (%file%) DO (

You need the usebackq directive to tell for that the quoted string is a filename, not a literal.

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

1 Comment

+1 I accepted the other answer for the simplicity, but this one is also good to remember.
0

You are telling it to read as a string. Move the quote before file and it should work.

set "file=tags.txt"

or just remove the quotes altogether.

1 Comment

This works, until the path or filename contains spaces or & characters. The following will always work for /f "delims=" %%a in ('type "c:\folder A\test file.txt" ') do echo(%%a as will the answer by Magoo.

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.