Using environment variables with replaceable parameters

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Bluejay
Posts: 8
Joined: 23 Feb 2017 13:33

Using environment variables with replaceable parameters

#1 Post by Bluejay » 27 Mar 2017 11:49

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.

:DR
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.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Using environment variables with replaceable parameters

#2 Post by aGerman » 27 Mar 2017 15:33

Either use the CALL command and double the outer percent signs ...
call echo %%R%#%%%

... or use delayed variable expansion and replace the outer percent signs with exclamation marks ...
setlocal EnableDelayedExpansion
echo !R%#%!
endlocal


Steffen

Bluejay
Posts: 8
Joined: 23 Feb 2017 13:33

Re: Using environment variables with replaceable parameters

#3 Post by Bluejay » 28 Mar 2017 13:38

:D
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.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Using environment variables with replaceable parameters

#4 Post by aGerman » 28 Mar 2017 13:47

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

Bluejay
Posts: 8
Joined: 23 Feb 2017 13:33

Re: Using environment variables with replaceable parameters

#5 Post by Bluejay » 29 Mar 2017 10:49

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.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Using environment variables with replaceable parameters

#6 Post by aGerman » 29 Mar 2017 11:02

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

Aacini
Expert
Posts: 1913
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Using environment variables with replaceable parameters

#7 Post by Aacini » 29 Mar 2017 11:31

I suggest you to read this SO post...

Or this one here.

Antonio

LotPings
Posts: 10
Joined: 26 Oct 2016 22:45

Re: Using environment variables with replaceable parameters

#8 Post by LotPings » 29 Mar 2017 13:40

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

Bluejay
Posts: 8
Joined: 23 Feb 2017 13:33

Re: Using environment variables with replaceable parameters

#9 Post by Bluejay » 30 Mar 2017 08:46

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?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Using environment variables with replaceable parameters

#10 Post by aGerman » 30 Mar 2017 13:55

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).

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

Bluejay
Posts: 8
Joined: 23 Feb 2017 13:33

Re: Using environment variables with replaceable parameters

#11 Post by Bluejay » 30 Mar 2017 19:42

Got it.
Works perfectly (as expected).
Thanks muchly.

Post Reply