Using environment variables with replaceable parameters
Moderator: DosItHelp
Using environment variables with replaceable parameters
Trying to display a series of stored environment variables.
For example:
Set R1=1.1
Set R2=2.2
Set R3=3.3
Set R#=3
Set #=1
Tried several variations of "for" commands. None worked.
Cannot echo variables containing replaceable parameter.
Echos of complete individual variables works fine,
e.g. echo:R%#%= %R1% - Shows R1= 1.1
But want loop process to handle varying
number of results depending on batch run.
So tried this.
R
Set R=R%#%
Echo:R%#%= %R%
If %R#%==%#% GoTo Done
Set /A R#=R#+1
GoTo DR
But echoes display R1, R2 ...
Not actual contents of variable.
Even tried Set W=R%#% (work variable)
and then Set R=W Same result.
Tried set /p method - causes pause and then same result.
Sort-of understand why this happens.
But can't find any way to fix problem.
For example:
Set R1=1.1
Set R2=2.2
Set R3=3.3
Set R#=3
Set #=1
Tried several variations of "for" commands. None worked.
Cannot echo variables containing replaceable parameter.
Echos of complete individual variables works fine,
e.g. echo:R%#%= %R1% - Shows R1= 1.1
But want loop process to handle varying
number of results depending on batch run.
So tried this.
R
Set R=R%#%
Echo:R%#%= %R%
If %R#%==%#% GoTo Done
Set /A R#=R#+1
GoTo DR
But echoes display R1, R2 ...
Not actual contents of variable.
Even tried Set W=R%#% (work variable)
and then Set R=W Same result.
Tried set /p method - causes pause and then same result.
Sort-of understand why this happens.
But can't find any way to fix problem.
Re: Using environment variables with replaceable parameters
Either use the CALL command and double the outer percent signs ...
... or use delayed variable expansion and replace the outer percent signs with exclamation marks ...
Steffen
call echo %%R%#%%%
... or use delayed variable expansion and replace the outer percent signs with exclamation marks ...
setlocal EnableDelayedExpansion
echo !R%#%!
endlocal
Steffen
Re: Using environment variables with replaceable parameters
Thank you very much, indeed.
Call echo works perfectly.
Completely unfamiliar with this technique.
Found no reference anywhere with many! hours searching many sites.
Very frustrating.
Note: Have used DOS batches for about 30 years.
And Norton techniques until no longer usable.
Have big DOS purchased manual... now buried somewhere since last move.
Can't even recall seeing Call method in manual.
Intend to do more research to find details, if possible.
Gotta ask- is there some workable "For" loop for this?
Guessing this would require setlocal EnableDelayedExpansion
Tried several variations. None worked.
Re: Using environment variables with replaceable parameters
Bluejay wrote:Gotta ask- is there some workable "For" loop for this?
Guessing this would require setlocal EnableDelayedExpansion
That's correct.
Code: Select all
setlocal EnableDelayedExpansion
for /f "delims=" %%i in ("!#!") do echo !R%%i!
endlocal
Re: Using environment variables with replaceable parameters
Thanks.
But this For command only echos 1 result (whatever # happens to be).
Did some searching about For.
After several attempts, finally figured out correct syntax:
For /F %%I in (1,1,!#!) Do Echo !R%%I!
This result was trial and error.
Will have to do much more research to truly
understand variable structure in for commands.
But this For command only echos 1 result (whatever # happens to be).
Did some searching about For.
After several attempts, finally figured out correct syntax:
For /F %%I in (1,1,!#!) Do Echo !R%%I!
This result was trial and error.
Will have to do much more research to truly
understand variable structure in for commands.
Re: Using environment variables with replaceable parameters
Oh you didn't mention that you wanted to enumerate all of your R... variables. So yes FOR /L is the right loop for this task.
Steffen
Steffen
Re: Using environment variables with replaceable parameters
aGerman wrote:Oh you didn't mention that you wanted to enumerate all of your R... variables. So yes FOR /L is the right loop for this task. Steffen
I'd say no, that iterates a compound variable name.
To enumerate an unknown number of variables starting with the letter R I suggest
Code: Select all
for /f "tokens=1*delims==" %%A in ('set R') Do Echo content of var %%A is %%B
Re: Using environment variables with replaceable parameters
Thanks again.
Last For command also works well. Also shows R# value before R# results.
Similar problem:
Trying to create D variable 1 with added zeros based on variable #.
Now using
Set #=4 (optional based on batch run)
Set D=1
:SETD
Set D=%D%0
Set /A #=#-1
If Not %#%==0 GoTo SetD
Tried
Setlocal EnableDelayedExpansion
For /f %%I in (1,1,!#!) Do Set D=%D%0
and
For /f "tokens=1*delims==" %%A in ('set #') Do Set D=%D%0
Endlocal
End result always = 10 even though For appears to execute (echo on shows 4 iterations).
What am I doing wrong?
Last For command also works well. Also shows R# value before R# results.
Similar problem:
Trying to create D variable 1 with added zeros based on variable #.
Now using
Set #=4 (optional based on batch run)
Set D=1
:SETD
Set D=%D%0
Set /A #=#-1
If Not %#%==0 GoTo SetD
Tried
Setlocal EnableDelayedExpansion
For /f %%I in (1,1,!#!) Do Set D=%D%0
and
For /f "tokens=1*delims==" %%A in ('set #') Do Set D=%D%0
Endlocal
End result always = 10 even though For appears to execute (echo on shows 4 iterations).
What am I doing wrong?
Re: Using environment variables with replaceable parameters
Normal variables will be expanded only once in a command line or in a block of command lines (enclosed into parentheses). This will be done before the line was even executed. To avoid this "early" expansion you have to enable delayed expansion (what you already did) and replace the percent signs with exclamation marks (what you didn't for variable D).
SETLOCAL creates a sub-environment. ENDLOCAL closes this sub-environment where all changes of variables are lost unless you define the variable with the former value in the same line (or block) containing the ENDLOCAL command.
Steffen
Code: Select all
@echo off
set /a "#=4, D=1"
setlocal EnableDelayedExpansion
for /l %%I in (1,1,!#!) do set "D=!D!0"
endlocal &set "D=%D%"
echo %D%
pause
SETLOCAL creates a sub-environment. ENDLOCAL closes this sub-environment where all changes of variables are lost unless you define the variable with the former value in the same line (or block) containing the ENDLOCAL command.
Steffen
Re: Using environment variables with replaceable parameters
Got it.
Works perfectly (as expected).
Thanks muchly.
Works perfectly (as expected).
Thanks muchly.