1.

Solve : for command question?

Answer»

This SEEMS to simple to be asking for help but here goes, help!
I'm running Windows7.

I'm writing a bat file with a for command that will open a TXT file containing several lines. Each line is a full path to a file that I want to copy to another location. The full path may CONTAIN a space character.
Example:
C:\Program Files\Java\jre6\README.txt

Here is the for command I am using:
FOR /F "usebackq tokens=1" %%A IN (C\temp\file_path.txt) DO (
echo File=%%~A
)

In file_path.txt I have the path above surrounded by double quotes ("").
Example:
"C:\Program Files\Java\jre6\README.txt"

When I run the bat file I get:

C:\Temp>FOR /F "usebackq tokens=1" %A IN (C:\temp\file_path.txt) DO (echo File=%~A )

C:\Temp>(echo File=C:\Program )
File=C:\Program

C:\Temp>pause

What am I missing that won't allow the for command to capture the entire line past the space character?

Thanks. Quote from: skibumsam on September 24, 2013, 10:18:34 AM

What am I missing that won't allow the for command to capture the entire line past the space character?

You are mistaken in thinking that tokens=1 will compress the whole line into the %%A metavariable. What the tokens=1 element does is take the line up to the first delimiter and put that first token into the EXPLICIT FOR metavariable (%%A in this case). If there were more tokens specified by numbers they would (assuming they were present in the line) be assigned to implicit metavariables starting with the next letter of the alphabet in the same case (here, %%B). Spaces and tabs are the fault delimiters if no other(s) is (are) specified using delims=. If you want to get the whole line into %%A you need to specify that the delimiter is the end of the line. That is, delims= followed by the closing double quote of the options BLOCK. You can do so like this

FOR /F "usebackq delims=" %%A IN (C:\temp\file_path.txt) DO (

By the way, in the filespec there needs to be a colon after the drive letter.

You are using usebackq when you don't need to.







Thanks.
That did it.


Discussion

No Comment Found