PC Review


Reply
Thread Tools Rate Thread

checking hte existence of an environment variable

 
 
Kok Yong Lee
Guest
Posts: n/a
 
      10th May 2006
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


 
Reply With Quote
 
 
 
 
Mark V
Guest
Posts: n/a
 
      10th May 2006
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)
 
Reply With Quote
 
Kok Yong Lee
Guest
Posts: n/a
 
      11th May 2006
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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)



 
Reply With Quote
 
kk
Guest
Posts: n/a
 
      11th May 2006
if "%asm_main_location%"=="" (
REM environment variable does not exist
..
) else (
REM environment variable exists
..
)


"Kok Yong Lee" <(E-Mail Removed)> schrieb im Newsbeitrag
news:%(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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)

>
>



 
Reply With Quote
 
Al Dunbar [MS-MVP]
Guest
Posts: n/a
 
      11th May 2006

"Kok Yong Lee" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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)

>
>



 
Reply With Quote
 
Al Dunbar [MS-MVP]
Guest
Posts: n/a
 
      12th May 2006

"Al Dunbar [MS-MVP]" <alan-no-drub-(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "Kok Yong Lee" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> > 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:


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" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > 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)

> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Checking for the existence of a variable in queryString Andy B. Microsoft ASP .NET 1 6th Feb 2009 09:37 AM
Checking for file existence John Microsoft VB .NET 5 22nd Sep 2005 08:04 PM
Checking for a key's existence Simon Microsoft Windows 2000 Registry Archive 6 18th Feb 2004 06:12 PM
Checking for a key's existence Simon Microsoft Windows 2000 Registry 3 18th Feb 2004 06:12 PM
Checking for a key's existence Simon Microsoft Windows 2000 Registry Archive 0 18th Feb 2004 03:46 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:49 AM.