FOR question

  • Thread starter Thread starter djc
  • Start date Start date
D

djc

I'm pretty sure I know the answer but I want to verify:

when using the FOR command (2k/xp/k3) and you need the 'command' that is
executed to actually be a block of commands (simple conditional processing
using IF) you need to make the 'command' call a seperate batch file.
Correct?

I just want to make sure there is not a way to put several lines of code
together right in the context of the FOR command before I go the seperate
batch file route. I would prefer keeping this task all in one file.... just
thought of this... could I use a GOTO command somehow and afterwards get
back into the body of the FOR statement where I left of before? probably
not. Just checking.

any info is appreciated. Thanks.
 
enclose the blocks of code in parenthesis like...

for /f "tokens=1-7" %%c in ('ftp -s:speedtest.script ^| findstr "/c:sent
in" "/c:received in"') do (
if !x!==0 (
set Speedtest=!Speedtest! %%iU 2>NUL
set /a x+=1
) Else (
if !x!==1 set Speedtest=!Speedtest! %%iD 2>NUL
)
)
 
Paul R. Sadowski said:
enclose the blocks of code in parenthesis like...

for /f "tokens=1-7" %%c in ('ftp -s:speedtest.script ^| findstr "/c:sent
in" "/c:received in"') do (
if !x!==0 (
set Speedtest=!Speedtest! %%iU 2>NUL
set /a x+=1
) Else (
if !x!==1 set Speedtest=!Speedtest! %%iD 2>NUL
)
)

"djc" <[email protected]> wrote

But you have to keep in mind that the variables in areas enclosed by
parenthes's are evaluated only once at beginning.
Paul shows how to get around this by using delayed expansion and using
the exclamation mark instead of the percent sign.

For details see: cmd /? and setlocal /?

For a different way with calling internal subs see call /?

When speed is an issue, delayed expansion is the preferred choice, but
as Michael Herz shows some threads back, there is a drawback with exclam.
marks in file names.
 
djc said:
I'm pretty sure I know the answer but I want to verify:

when using the FOR command (2k/xp/k3) and you need the 'command' that is
executed to actually be a block of commands (simple conditional processing
using IF) you need to make the 'command' call a seperate batch file.
Correct?

I just want to make sure there is not a way to put several lines of code
together right in the context of the FOR command before I go the seperate
batch file route. I would prefer keeping this task all in one file.... just
thought of this... could I use a GOTO command somehow and afterwards get
back into the body of the FOR statement where I left of before? probably
not. Just checking.

any info is appreciated. Thanks.

You can execute a block of commands as the target of a FOR statement and
stay within the same script.

FOR /F "tokens=* delims=" %%A IN ('Command1') DO @(
(SET "abc=%%A")
(CALL :Part2)
)
GOTO :EOF

:Part2

%.Rem% The value of variable "abc" is now available as "%abc%"
Command2
Command3
GOTO :EOF

*******
Or, within the same logical line of code:

FOR /F "tokens=* delims=" %%A IN ('Command1') DO @(
(SET "abc=%%A")
(%.ForExpand% %%B IN ('SET abc') DO @(
(%.Rem% The contents of variable "abc")
(%.Rem% _are now available as "%%C")
(Command2)
(Command3)
)
)

*******

The .Mount/\Command ".ForExpand" will expand variables
at execution time, providing the same functionality as
"Delayed Expansion", without the hassle.

ForExpand works CONSISTENTLY under NT/2K/XP/K3 using the syntax

%.ForExpand% %%A IN ('SET VariableName') DO (Command %%B)

%%A = VariableName
%%B = Dynamically calculated CONTENTS of VariableName

For a color-keyed example, see
(http://TheSystemGuard.com/MtCmds/CrystalClear/ForExpand.htm)

*******

The .Rem command is a "Mounted" version of the builtin REM command. The
main advantages are:

1. .Rem can be used ANYWHERE in a script that a
command is expected without causing an error.

2. Comments made using .Rem can be displayed
during script execution for troubleshooting.
This greatly enhances your ability to write
complex statements while retaining a complete
understanding of how the code works.

Display of .Rem Comments can be toggled on or off
using %.ShowComments% and %.HideComments%. For a
color-keyed example, see
(http://TheSystemGuard.com/MtCmds/CrystalClear/Rem.htm)

*******

All of the Mount/\Commands discussed are part of the (FREE) Advanced Command
Library, which contains over 200 resources to help you write
self-documenting and maintainable cross-platform (NT/2K/XP/K3) shell
scripts.
(http://ntlib.com)

NOTE: A .Mount/\Command is simply an optimized
section of scripting code, stored in an
environment variable for immediate access,
with a "sounds like what is does" name.

*******

-tsg

/-----------------+---------------+----------------------\
| COMPATIBILITY | CLARITY | SPEED |
| Write code ONCE | Make it clear | THEN...Make it fast! |
\-----------------+---------------+----------------------/
400+ command-line resources using ONLY native NT commands!
(http://TheSystemGuard.com/default.asp#MasterCommandList)
 
Back
Top