Reading lines from a text file in batch file
I know this has been asked 100 times before but every bit of code I try just doesn't seem to work. I am trying to compile a file and then check for a error file and read it line by line and dump it into command line:
This is what I have so far:
Set var1= %1
Set var2= %var1:.mb=.ERR%
echo %var1%
echo %var2%
"C:\Program Files\MapInfo\MapBasic\mapbasic.exe" -D %var1%
FOR /f "delims=" %%a in (%var2%) do echo %%a
So I am taking the path to the file as a command line a开发者_运维问答rg this works fine, then taking the .mb off and replacing it with .ERR and running the program to complie the .mb file, everything works fine up until I hit the for loop.
The result for loop looks like this, when running with echo on:
FOR /F "delims=" %a in ("C:\Documents and Settings\woodrown\My Documents\Visual Studio 2008\Projects\MapInfoInteropTemplate1\MapInfoInteropTemplate1.ERR") do echo %a
but when it runs it just prints out
"C:\Documents and Settings\woodrown\My Documents\Visual Studio 2008\Projects\MapInfoInteropTemplate1\MapInfoInteropTemplate1.ERR"
not the files content.
I can get it to work if I give it a hard name and no path with spaces, such as 'MapInfoInteropTemplate1.ERR' but I want it to use %var2%
for the file name to read.
If I put '
around the path it just launches my default text editor.
I ended up skipping the whole batch file generation of computer programing so this is really strange to me, sorry if it's a basic question.
Use the usebackq
option to for
:
FOR /F "usebackq delims=" %%a in ("C:\Documents and Settings\woodrown\My Documents\Visual Studio 2008\Projects\MapInfoInteropTemplate1\MapInfoInteropTemplate1.ERR") do echo %%a
Otherwise it will interpret whatever you put between double-quoets as a string to tokenize, not as a file to read. This is also stated in the help:
or, if usebackq option present:
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]
精彩评论