0

I'm trying to access a file and read its content in a nested loop but the inner loop cannot access the file.. (in batch)

Here's the code (a small part of the entire script):

for %%b in (!directory!) do (
   echo File used: %%b
   for /f "delims= " %%c in (%%b) do (
      echo %%c
   )
)

The problem is:

if "%%b" equals "C:\Documents and settings\test\test.txt", the inner loop will try to access to "C:\Documents" (because of the space). If I put double-quotes around "%%b", it will parse it as a string and not the file itself.

How can I deal with that? The file is dynamic, I don't know its name...

Thanks

el_grom
  • 163
  • 1
  • 3
  • 15

1 Answers1

2

You need to use the USEBACKQ option when using FOR /F to read a file with spaces in the name. The USEBACKQ changes the semantics of the various quotes. Normally no quotes means file, double quotes means string, and single quotes means command. The USEBACKQ option modifies such that no quotes or double quotes means file, single quotes means string, and back quotes means command. Type HELP FOR from the command prompt for more information.

for %%b in (!directory!) do (
   echo File used: %%b
   for /f "usebackq delims= " %%c in ("%%b") do (
      echo %%c
   )
)
dbenham
  • 127,446
  • 28
  • 251
  • 390