|
| |
.ForExpand
Description
- Dynamically expand the contents of a variable
- Allows the contents of a variable to be set or modified within a complex statement, and then have that updated contents be available for processing within the same statement.
(similar to "Delayed Variable Expansion" under 2K/XP/K3,
but ForExpand DOES NOT require any special settings
AND it is CONSISTENT across NT/2K/XP/K3.)
- Syntax is the same as the internal command FOR.
Syntax
|
1 |
%.ForExpand% |
|
%%A
IN ('SET
VariableName') DO (
ECHO:VariableName=%%A,ExpandedContents=%%B) |
|
2 |
%.ForExpand% |
|
%%A
IN ('SET
| FindStr /b "VariableName="')
DO (
ECHO:VariableName=%%A,ExpandedContents=%%B) |
1 is slightly faster by using only internal commands, but is
susceptible to problems if multiple variables begin with "VariableName" (i.e., VariableName1,
VariableName2, etc.). Also, if it is possible that VariableName does not exist, prefix
SET VariableName with
%.eKity% to suppress the "Environment variable xxx not found" error.
|
%.ForExpand% |
|
%%A
IN ('%.eKity%
SET VariableName')
DO (
ECHO:VariableName=%%A,ExpandedContents=%%B) |
2 is slightly slower but will always find ONLY the variable you are looking
for.
Parameters and Switches
Errorlevels and Output Variables
|
FOR
Variable |
|
Value of
Variable |
|
|
|
Description of Result |
|
%%A |
|
VariableName |
|
|
|
Name of the variable to be expanded. |
|
%%B |
|
ExpandedContents |
|
|
|
Dynamically expanded contents of the
variable named in %%A. |
Examples, Notes and Instructions
ForExpand_Demo.cmd
01. @ECHO OFF
02.
SETLOCAL ENABLEEXTENSIONS
03.
04. SET Count=0
05. ECHO:Initial Value of Count=%Count%
06.
07.
%.DashLine%
08. ECHO:Without using
ForExpand, the variable "Count" is
09. ECHO:is evaluated once, at the beginning of Line 12.
10.
%.DashLine%
11.
12. FOR /L %%Z IN (1,1,5) DO @(
13. (SET Count=%%Z)
14. (ECHO:%Count%)
15. )
16.
17.
%.DashLine%
18. ECHO:When using ForExpand,
the contents of variable
19. ECHO:"Count" are expanded dynamically EACH TIME
20. ECHO:Line 25 is executed
and made available as %%B.
21.
%.DashLine%
22.
23. FOR /L %%Z IN (1,1,5) DO @(
24. (SET Count=%%Z)
25. (%.ForExpand%
%%A IN ('SET
Count') DO @(
26. ECHO:%%B)
27. )
28. )
29.
30.
%.UnderLine%
31.
%.dtEcho% End of Script [%~nx0]
NT
2K
XP
K3 |
C:\GuardPost>ForExpand_Demo
Initial Value of Count=0
-----------------------------------------------------------------------------
Without using ForExpand, the variable "Count" is
is evaluated once, at the beginning of Line 12.
-----------------------------------------------------------------------------
0
0
0
0
0
-----------------------------------------------------------------------------
When using ForExpand, the contents of variable
"Count" are expanded dynamically EACH TIME
Line 25 is executed and made available as %B.
-----------------------------------------------------------------------------
1
2
3
4
5
_____________________________________________________________________________
[Sat 03/15/2003 18:26:03] End of Script [ForExpand_Demo.cmd]
C:\GuardPost> |
- Output is displayed to STDOUT
- Any two sequential letters may be used in place of %%A and %%B. The second letter will always evaluate to the expanded contents of the variable(s) specified.
Related .Mount/\Commands, #Constants,
$Functions, :Procedures
|
Resource |
|
Short Description |
|
.ForAll |
|
Place the entire output of a command in a FOR
variable. |
|
K
E
Y |
Common Commands |
INTERNAL |
External |
/SWITCH |
Parameter |
Help text |
AddonTool |
|
Mounted Commands |
.Mount/\Command |
CmdShorthand |
#Constant |
$FUNCTION |
:Procedure |
!GuardNote |
|
Operating Systems |
NT/2K/XP/K3 |
NT Only |
NT/2K |
2K Only |
2K/XP |
XP Only |
XP/K3 |
K3 Only |
2K/XP/K3 |
|