Syntax Check

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Folks:


Appreciate if someone could correct the syntax of this batch file. This
batch file just won't run properly.
 
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%)
-------------------------------------------------------------------------

Untested:

@echo off
FOR /F "delims=" %%I IN (C:\MyTextFile.txt) DO (
SET VAR=%%I
ECHO inside the loop: %%I)
echo outside the loop var is %var%
pause
 
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
 
Foxidrive:

Thanks for you solution. It works well. I did some additional tests and
these issues came up.

(1) The script works without "delims". What is the purpose of "delims"
?

FOR /F %%I IN (C:\MyTextFile.txt) DO (
SET VAR=%%I
ECHO %VAR%
C:\Program Files\MyEXEfile.exe)

(2) I have always used "START /WAIT" in my scripts. The idea is to
allow certain tasks to complete before looping again
Now, however, this command is actually preventing the main
script from working. Is it wise to use "START /WAIT" .


Thanks again,
JoJo.
 
Thanks for you solution. It works well. I did some additional tests and
these issues came up.

(1) The script works without "delims". What is the purpose of "delims"?

If the contents of the file has lines with spaces/tabs then "delims=" will
ensure that the entire line is returned instead of only the initial portion
(the command meaning there are no delimiters used when processing the
lines)
FOR /F %%I IN (C:\MyTextFile.txt) DO (
SET VAR=%%I
ECHO %VAR%
C:\Program Files\MyEXEfile.exe)

To SET and use a variable *within* a for-in-do loop requires the use of the
"setlocal enabledelayedexpansion" line and associated !var! commands,
otherwise the variable is not updated until the loop has finished. A
workaround is to call and use a subroutine.
(2) I have always used "START /WAIT" in my scripts. The idea is to
allow certain tasks to complete before looping again
Now, however, this command is actually preventing the main
script from working. Is it wise to use "START /WAIT" .

start /wait is used for multi-threaded executables which will launch a task
and return to the batch file immediately. If you need it within the loop
for such an executable then you can use it, but most vanilla batch commands
are single threaded and don't require start /wait (or start /w).
 
foxidrive said:
On Tue, 25 Mar 2008 07:30:00 -0500, <JoJo> wrote:


start /wait is used for multi-threaded executables which will launch a
task
and return to the batch file immediately. If you need it within the loop
for such an executable then you can use it, but most vanilla batch
commands
are single threaded and don't require start /wait (or start /w).

As I read it, the problem is not start/wait and whether or not all vanilla
batch commands (external command executables?) are single-threaded, but what
purpose would be served in running a literal for loop that is supposed to
set an environment variable as a separate process with "start", when the
variables defined in that separate process would not be transmitted back to
the calling batch script.

/Al
 
Back
Top