The documentation that you get when you type help for tells you what to do if you have a path with spaces.
For file names that contain spaces, you need to quote the filenames with double quotes. In order to use double quotes in this manner, you also need to use the usebackq option, otherwise the double quotes will be interpreted as defining a literal string to parse.
The default FOR /F syntax is as follows.
FOR /F ["options"] %variable IN (file-set) DO command [command-parameters] FOR /F ["options"] %variable IN ("string") DO command [command-parameters] FOR /F ["options"] %variable IN ('command') DO command [command-parameters]
This syntax shows why the type workaround works. Because single quotes are said to execute the type command and loop through its output. When you add the usebackq parameter, the syntax changes to this:
FOR /F ["options"] %variable IN (file-set) DO command [command-parameters] FOR /F ["options"] %variable IN ('string') DO command [command-parameters] FOR /F ["options"] %variable IN (`command`) DO command [command-parameters]
Now you specify the file paths twice, literal strings with one quotation mark, and place reverse feedback signals (serious accents) around the commands that are executed.
So you want to do this:
for /f "usebackq" %%a in ("C:\Program Files\myfolder\myfile.txt") do ( echo %%a )
indiv
source share