noway
noway

Reputation: 2585

Variable is ignored in batch script

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?

Upvotes: 0

Views: 64

Answers (2)

Magoo
Magoo

Reputation: 80203

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.

Upvotes: 2

Matt Williamson
Matt Williamson

Reputation: 7105

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.

Upvotes: 0

Related Questions