Folks:
Appreciate if someone could correct the syntax of this batch file. This
batch file just won't run properly.
-------------------------------------------------------------------------
START /WAIT FOR /F %%I IN (C:\MyTextFile.txt) DO (
SET VAR=%%I
@ECHO %VAR%)
By "properly" I take it you mean as you expect.
Although you don't tell us what you expect, the root cause of your problem
is that the parser substitutes for %var% and THEN executes the command,
hence you batch line is actually executed as
START /WAIT FOR /F %%I IN (C:\MyTextFile.txt) DO (
SET VAR=%%I
@ECHO originalvalueofvar)
which is evidently not what you want.
We have to read between the lines, and this code seems to be just one line
from a larger batch.
First, the START /WAIT seems to have no purpose.
Next, %%I will be allocated the first token from each line of the file, not
the entire line. Is this what you want? If you want each entire line, then
you need to use
for /f "tokens=*" ....
or
for /f "delims=" ...
Next, have you put the "@" before the ECHO in an attempt to suppress the
"ECHO IS OFF" message? If so, try ECHO;%var%
Last, since the parser performs the substitution before the command is
executed, you'd need to use the delayed-expansion mode
setlocal enabledelayedexpansion
START /WAIT FOR /F "tokens=*" %%I IN (C:\MyTextFile.txt) DO (
SET VAR=%%I
ECHO;!var!)
Note the use of !var! which is the dynamically-allocated value of VAR when
you are using delayed expansion.
Unfortunately, delayed-expansion is an option of the SETLOCAL command, so
VAR will be restored to its original value (the value as it stood on
executng the SETLOCAL) at the end of the script. We don't know whether that
i ssignificant since you don't reveal what you're doing.
Refs:
From the prompt,
FOR /?
SETLOCAL /?
for help on the FOR and SETLOCAL commands - or look at alt.msdos.batch.nt
for examples - where this delayed-expansion problem is the #2FAQ