user4350786
user4350786

Reputation:

Batch File - Read from a text file with spaces

I wrote a batch file to read a text file located in a specific path. The for statement reads from a text file located in a folder. If this same path had spaces the for can't read it. Even if I wrap the path in quotes it still can't read it. The path does exist, I checked multiple times, and confirmed that for only works with pathnames that contain no spaces.

Is there something that I'm doing wrong, or, is it a limitation of the batch for statement?

Code:

FOR /F "tokens=* delims=" %%x in (C:\Users\someuser\VirtualBoxLog\log.txt) DO SET read=%%x

If this path contained a space, for example C:\Users\someuser\VirtualBox Log\log.txt it can't read from the text file, even if I wrap it in quotes and the folder "VirtualBox Log" existed.

Upvotes: 1

Views: 2867

Answers (1)

Stephan
Stephan

Reputation: 56180

The solution for your problem is enclosing the path in quotes:

"C:\Users\someuser\VirtualBox Log\log.txt"

But now another problem rises: for interprets a quotet string as a string, not as a filename.

To work around this, use the usebackq keyword:

FOR /F "usebackq tokens=* delims=" %%x in ("C:\Users\someuser\VirtualBox Log\log.txt") DO SET read=%%x

Upvotes: 2

Related Questions