1

hi I'm new to batch scripting when is use address of the of inside the for loop there is no problem but when is store that address in a variable and use that variable in a for /f loop for loop read the text of the variable not the file content

set file1="c:\a.txt"

FOR /F "tokens=* delims=" %%x in (%file1%) DO ( 
    echo %%x
) 

2 Answers 2

2

Quoting a filename is a good idea to avoid problems with spaces, but for /f processes a quoted string within the parentheses as a literal string, unless you use the usebackq option.

Best practice is to quote the string where needed instead of including the quotes to the string, so instead of set file1="c:\a.txt", use set "file1=c:\a.txt" (note the position of the quotes).

set "file1=c:\a.txt"
for /f "usebackq tokens=* delims=" %%x in ("%file1%") do (
  echo %%x
)
Sign up to request clarification or add additional context in comments.

Comments

1

This is because of the quotes in variable value - cmd.exe takes it for a string. Try using usebackq and using quotes in set expression like in the example bellow:

set "file1=c:\a.txt"

for /f "usebackq tokens=* delims=" %%x in ("%file1%") do (
  echo %%x
)

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.