DOS environmental variables within parentheses

G

Guest

Is the following a bug or a feature? Consider the following short MS DOS
batch script from a routine for converting latitude and longitude values into
Flinn-Engdahl region names. Although both a and b are set within the scope of
the ( )'s, references to their values are not correctly resolved until
outside of the ( )'s even though a set without arguments from within the (
)'s shows that they are indeed defined correctly.

==== test.bat
======================================================================================

@echo off
if 1 == 1 (
set a=1
set b=2
set
echo a=%a%
echo b=%b%
)
echo a=%a%
echo b=%b%


***** Condensed output from the set showing that a and b are defined within
the ( )'s:

a=1
ALLUSERSPROFILE=C:\Documents and Settings\All Users
b=2
CommonProgramFiles=C:\Program Files\Common Files
.... etc.

***** References to a and b within the ( )'s DO NOT pick up the values of a
and b:

a=
b=

***** References to a and b outside of the ( )'s DO pick up the values of a
and b:

a=1
b=2
 
P

Pegasus \(MVP\)

beetle said:
Is the following a bug or a feature? Consider the following short MS DOS
batch script from a routine for converting latitude and longitude values into
Flinn-Engdahl region names. Although both a and b are set within the scope of
the ( )'s, references to their values are not correctly resolved until
outside of the ( )'s even though a set without arguments from within the (
)'s shows that they are indeed defined correctly.

==== test.bat
============================================================================
==========

@echo off
if 1 == 1 (
set a=1
set b=2
set
echo a=%a%
echo b=%b%
)
echo a=%a%
echo b=%b%


***** Condensed output from the set showing that a and b are defined within
the ( )'s:

a=1
ALLUSERSPROFILE=C:\Documents and Settings\All Users
b=2
CommonProgramFiles=C:\Program Files\Common Files
... etc.

***** References to a and b within the ( )'s DO NOT pick up the values of a
and b:

a=
b=

***** References to a and b outside of the ( )'s DO pick up the values of a
and b:

a=1
b=2

You claim that references to the values of %a% and %b% are
not set correctly. They are actually set correctly. Your batch
file consists essentially of one single line even though it is
broken into several lines. Here is that line:

if 1 == 1 (set a=1 & set b=2 & echo a=%a% & echo b=%b%)

This line is scanned ***once*** before being executed. At
that time no values are assigned to %a% and %b%, hence
they appear as blanks.

You need to apply a different syntax if you want to force a
rescan inside a single line:

@echo off
setlocal enabledelayedexpansion
if 1 == 1 (
set a=1
set b=2
echo a=!a!
echo b=!b!
)
endlocal

You can achieve the same effect by resorting to subroutines:

@echo off
if 1==1 call :sub1
goto :eof

:sub1
set a=1
set b=2
echo a=%a%
echo b=%b%
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top