Question on formatting?

  • Thread starter Thread starter LWG
  • Start date Start date
L

LWG

I have been using posts to help tutor me in using the for command. I do have
a question about the do statement. Is that a hard return after the ( ? How
does for interpret that part?
 
If you include the (, then it is followed by a hard return.

for /f "Tokens=*" %%b in ('type file.txt') do set line= %%b&call :parse

can be wtitten as

for /f "Tokens=*" %%b in ('type file.txt') do (
set line= %%b
call :parse
)





I have been using posts to help tutor me in using the for command. I do have
a question about the do statement. Is that a hard return after the ( ? How
does for interpret that part?


Jerold Schulman
Windows: General MVP
JSI, Inc.
http://www.jsiinc.com
 
Jerold said:
If you include the (, then it is followed by a hard return.

for /f "Tokens=*" %%b in ('type file.txt') do set line= %%b&call :parse

can be wtitten as

for /f "Tokens=*" %%b in ('type file.txt') do (
set line= %%b
call :parse
)
Adding to Jerolds good tip,
depending on the length of the commands it might come handy to write:

for /f "Tokens=*" %%b in (
'type file.txt'
) do (
set line= %%b
call :parse
)

And if you can't split at that points put an up arrow as the last char ^
Cmd.exe understands that the command will continue on the next line.
 
Matthias Tacke said:
Adding to Jerolds good tip,
depending on the length of the commands it might come handy to write:

for /f "Tokens=*" %%b in (
'type file.txt'
) do (
set line= %%b
call :parse
)

And if you can't split at that points put an up arrow as the last char ^
Cmd.exe understands that the command will continue on the next line.

Adding to Jerold & Matthais good tips, and answering your question "How does
for interpret that part?":

FOR doesn't interpret that part. Parentheses are used at a lower level to
group commands together almost as if they were a single command. You often
see this with FOR and IF commands, as this syntax also helps make code more
readable. Having said that, of course, the FOR command *DOES* interpret the
parenthesized string between the "in" and "do" tokens.

Parenthesized compound commands don't need FOR or IF, though. Consider the
following:

(
echo/where am I?
cd
echo/what files are here?
dir
) >tmp.txt

Seems a better and simpler construct than:

echo/where am I?>tmp.txt
cd>>tmp.txt
echo/what files are here?>>tmp.txt
dir>>tmp.txt

/Al
 
Al Dunbar said:
Parenthesized compound commands don't need FOR or IF, though. Consider the
following:

(
echo/where am I?
cd
echo/what files are here?
dir
) >tmp.txt

Full Ack, that also comes in handy while debugging, if you set echo on
at begin you may group commands with an

@(
echo/where am I?
cd
echo/what files are here?
dir
)

To avoid the debugging being to noisy :-)
 
Back
Top