I need to use a cmd.exe command line (cmd.exe is being called from the gyp build tool) to determine whether an environment variable is defined or not. How can I do this? I am okay assuming that the variable value does not contain single or double quotes, but cannot assume that command extensions are enabled.
I've tried the following, which works great in a .bat file, but fails when typed directly on the command line:
IF "%UNDEFINED%" == "" (echo yes)
When that exact line is in a .bat file and executed, I see yes
as the output. When I type it on the command line, the output is empty. I am testing this on Windows XP SP3, though my coworker sees the same results on Windows 7. This is the method suggested by http://support.microsoft.com/kb/121170 and http://www.robvanderwoude.com/battech_defined.php. I do not want to use IF DEFINED UNDEFINED (echo yes)
because that won't work if command extensions are disabled.
The top-voted answer in the following post has led me to believe that this issue is related to how percent-expansion is handled differently in the "CmdLineParser" vs. the "BatchLineParser," but still has not led me to a solution: How does the Windows Command Interpreter (CMD.EXE) parse scripts?
SET
command to check if a variable is defined. – RBarryYoungIF (SET UNDEFINED) (echo yes)
and gotUNDEFINED) was unexpected at this time.
– JohannIF "%VARIABLE%" == ""
syntax, it never evaluates to true, even when VARIABLE is not defined. I'm assuming it's because it's running through the same command line parser.IF DEFINED
does work as expected, but I cannot guarantee that command extensions will be enabled. – Johannset
with no args to print the environment, and usefind
to find the variable. I was going to post an answer with the same. But because of Barry's answer, it simply would have been a "me too" answer. – jww