if defined <variable>
works just fine.
This is not true for command line variables.
if defined %1
and
if defined 1
do not have the desired result.
if "%1"==""
is not safe if %1 has poison characters in it.
The only way I have found to do it is
set ENV=%1
if defined ENV
but this mucks with the environment so to be safe you have to
"setlocal/endlocal" it.
Am I missing someother trick?
Thanks,
Cyberclops
> if "%1"==""
>
>is not safe if %1 has poison characters in it.
This works preety well
if "%~1"==""
> if "%1"==""
The suggestion already given is good.
> set ENV=%1
> if defined ENV
>
> but this mucks with the environment so to be safe you have to
> "setlocal/endlocal" it.
True and false. Assuming you intended to do something like this:
If DEFINED %1 (
REM Use %1.
)
Then instead of SETLOCAL you could
Set "ENV=%~1"
If DEFINED ENV (
REM Use ENV.
Set "ENV="
)
> Am I missing someother trick?
We all are.
Here is another option:
Echo.%1|FindStr .&& Echo DEFINED
Frank
> As far as you idea
>
> set "ENV=%~1"
> if DEFINED ENV (
> REM Use ENV.
> set "ENV="
> )
>
> This is only advisable if you know the ENV is not already
> defined.
> That is what the setlocal/endlocal is needed.
You are correct.
If "ENV" already exists in the environment then the statement 'SET
"ENV=%~1"' would undefine it if "%1" EQU "". So the test works as
intended but it looses any previous value of ENV. This could be a
problem if your script is running in an environment that is common to
other processes and the variable you choose for the test ("ENV" in this
example) is important in one of those other processes. To avoid this
problem SETLOCAL should be used as you stated.
Frank