To read lines from a file, in a batch file, you do :
for /f %%a in (myfile.txt) do (
:: do stuff...
)
Now suppose you file is in C:\Program Files\myfolder
for /f %%a in ("C:\Program Files\myfolder\myfile.txt") do (
echo %%a
)
Result :
C:\Program Files\myfolder\myfile.txt
This seems to interpret the given path as a string, and thus %%a
is your given path.
Nothing about this in the documentation I have found so far. Please someone help me before I shoot myself.
The documentation you get when you type
help for
tells you what to do if you have a path with spaces.By default, the syntax of
FOR /F
is the following.This syntax shows why your
type
workaround works. Because the single quotes say to execute thetype
command and loop over its output. When you add theusebackq
option, the syntax changes to this:Now you double quote paths to files, single-quote literal strings, and put backticks (grave accents) around commands to execute.
So you want to do this:
Found it.
Don't even ask me why that works.
Just sharing the below code, hoping that somebody will get benefited.
The below code take both the path having spaces and also if the read lines has spaces, it wont cause any issue of the characters after space is missing;
FOR /f "tokens=* delims=," %%a in ('type "C:\Progrem File\My Program"') do ( echo %%a )