checking hte existence of an environment variable

  • Thread starter Thread starter Kok Yong Lee
  • Start date Start date
K

Kok Yong Lee

Hi there,

Would like to know what is the best way to determine whether an environment
variable has been set in a batch file. I dont care about its value just
wanted to know whetehr user has previously set it.

thanks in advanced
 
In said:
Hi there,

Would like to know what is the best way to determine whether an
environment variable has been set in a batch file. I dont care
about its value just wanted to know whetehr user has previously
set it.

thanks in advanced

IF /?

"The DEFINED conditional works just like EXISTS except it takes an
environment variable name and returns true if the environment
variable is defined." (W2K)
 
Thanks Mark

However, with the new IF DEFINED loop, I find it the batch script behave
differently. For example, in the code snippet below, if I detected env
variable asm_main_location is set, I will call another batch file to set
some env varible then use the env varible to create a folder.

With the IF DEFINED loop, for some reason the %ASMFULL% env variable dont
get recognise and hence no folder is created, but at the end of the batch
script I can clear see that %ASMFULL% is set via SET ASMFULL.

And if I comment out the IF DEFINED loop, then the batch script will go
ahead and create a new folder based on the ASMFULL value.

any ideas what may have been wrong with my script?

set asm_main_location=blah
echo [Setting 3rd Party Environment]
if DEFINED asm_main_location (
call R:\build\versions.bat /o
if not exist %_INV_3RD_PARTY_LOCALDIR%\ASM\%ASMFULL% (
mkdir %_INV_3RD_PARTY_LOCALDIR%\ASM\%ASMFULL%
) else (
echo Folder %_INV_3RD_PARTY_LOCALDIR%\ASM\%ASMFULL% existed already
)
)
echo [Finish Setting Environment]
 
if "%asm_main_location%"=="" (
REM environment variable does not exist
..
) else (
REM environment variable exists
..
)


Kok Yong Lee said:
Thanks Mark

However, with the new IF DEFINED loop, I find it the batch script behave
differently. For example, in the code snippet below, if I detected env
variable asm_main_location is set, I will call another batch file to set
some env varible then use the env varible to create a folder.

With the IF DEFINED loop, for some reason the %ASMFULL% env variable dont
get recognise and hence no folder is created, but at the end of the batch
script I can clear see that %ASMFULL% is set via SET ASMFULL.

And if I comment out the IF DEFINED loop, then the batch script will go
ahead and create a new folder based on the ASMFULL value.

any ideas what may have been wrong with my script?

set asm_main_location=blah
echo [Setting 3rd Party Environment]
if DEFINED asm_main_location (
call R:\build\versions.bat /o
if not exist %_INV_3RD_PARTY_LOCALDIR%\ASM\%ASMFULL% (
mkdir %_INV_3RD_PARTY_LOCALDIR%\ASM\%ASMFULL%
) else (
echo Folder %_INV_3RD_PARTY_LOCALDIR%\ASM\%ASMFULL% existed already
)
)
echo [Finish Setting Environment]





Mark V said:
IF /?

"The DEFINED conditional works just like EXISTS except it takes an
environment variable name and returns true if the environment
variable is defined." (W2K)
 
Kok Yong Lee said:
Thanks Mark

However, with the new IF DEFINED loop, I find it the batch script behave
differently. For example, in the code snippet below, if I detected env
variable asm_main_location is set, I will call another batch file to set
some env varible then use the env varible to create a folder.

With the IF DEFINED loop, for some reason the %ASMFULL% env variable dont
get recognise and hence no folder is created, but at the end of the batch
script I can clear see that %ASMFULL% is set via SET ASMFULL.

And if I comment out the IF DEFINED loop, then the batch script will go
ahead and create a new folder based on the ASMFULL value.

any ideas what may have been wrong with my script?

just before the call r:\build\version.bat /o command is executed, ALL of the
variables up to the matching ")" are expanded. You may change their values,
either directly or indirectly via a call to another batch. What you need to
do is replace those "%" signs with "!", and add the following command at the
top of the batch file:

setlocal enabledelayedexpansion

/Al

set asm_main_location=blah
echo [Setting 3rd Party Environment]
if DEFINED asm_main_location (
call R:\build\versions.bat /o
if not exist %_INV_3RD_PARTY_LOCALDIR%\ASM\%ASMFULL% (
mkdir %_INV_3RD_PARTY_LOCALDIR%\ASM\%ASMFULL%
) else (
echo Folder %_INV_3RD_PARTY_LOCALDIR%\ASM\%ASMFULL% existed already
)
)
echo [Finish Setting Environment]





Mark V said:
IF /?

"The DEFINED conditional works just like EXISTS except it takes an
environment variable name and returns true if the environment
variable is defined." (W2K)
 
Al Dunbar said:
just before the call r:\build\version.bat /o command is executed, ALL of the
variables up to the matching ")" are expanded. You may change their values,
either directly or indirectly via a call to another batch. What you need to
do is replace those "%" signs with "!", and add the following command at the
top of the batch file:

Oooops, typo alert. I meant to say above "... via a call to another batch,
but you will not be able to display those new values from within the "( )"
block.

/Al
setlocal enabledelayedexpansion

/Al

set asm_main_location=blah
echo [Setting 3rd Party Environment]
if DEFINED asm_main_location (
call R:\build\versions.bat /o
if not exist %_INV_3RD_PARTY_LOCALDIR%\ASM\%ASMFULL% (
mkdir %_INV_3RD_PARTY_LOCALDIR%\ASM\%ASMFULL%
) else (
echo Folder %_INV_3RD_PARTY_LOCALDIR%\ASM\%ASMFULL% existed already
)
)
echo [Finish Setting Environment]





Mark V said:
In microsoft.public.win2000.cmdprompt.admin Kok Yong Lee wrote:

Hi there,

Would like to know what is the best way to determine whether an
environment variable has been set in a batch file. I dont care
about its value just wanted to know whetehr user has previously
set it.

thanks in advanced

IF /?

"The DEFINED conditional works just like EXISTS except it takes an
environment variable name and returns true if the environment
variable is defined." (W2K)
 
Back
Top