Skip to content
⌘ NSIS Forum Archive

How to detect if an environment-variable exists?

4 posts

MPyle#

How to detect if an environment-variable exists?

Hi,
( I'm new to NSIS, but I've searched the wiki & faq.
Many examples show reading & setting env-vars. )

How do I find out if an environment varible is set/exists?

The expression: $%MyOptionalEnv% will return the env-var's value, if the env-var exists.
But can I use !ifdef on that in some way?

If the env-var is not set, i understand it will have a value equal to its symbolic-text. But if I do a value compare with "$%MyOptionalEnv%" i can't make it work correctly.

This could be my being dumb, and not being able to escape strings correctly.

What is the best way to do this?

Many Thanks
Anders#
You could try !if "$%foo%" != "$$%foo%" , if that does not work, you are going to have to use !system
jpderuiter#
No, "$$%foo%" does not work.
This is how I solved it:
!searchreplace MyOptionalEnv "$%MyOptionalEnv%" "%" "#"
!if "${MyOptionalEnv}" == "$#MyOptionalEnv#"
!echo "MyOptionalEnv not found"
!else
!echo "MyOptionalEnv found: ${MyOptionalEnv}"
!endif
Please note that this check runs at compile time, but since you mentioned $%MyOptionalEnv%, I assumed that is what you want.
MPyle#
Well it is "tad ugly" but it works! :-)

Thanks!

(sorry for late thanks, didn't get chance to try it for ages)