Check if an environment variable is defined without command extensions and without using a batch file?

后端 未结 5 535
南笙
南笙 2020-12-25 10:54

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

5条回答
  •  再見小時候
    2020-12-25 11:14

    IF NOT %CODE%==? do stuff.
    

    This works on a W98 command line and in a batch file, so it ought to work anywhere from early MS-DOS onwards with no extensions needed. It assumes that CODE is usefully set or not set at all.

    It results in a syntax error if CODE does not exist, or does nothing if CODE is a question mark (chosen because it could never exist in a path). Either way, nothing is done. The NOT makes sure action is only taken if CODE appears to be set to something useful.

    I use it in compiler batch files to determine whether to use relative paths or use a fixed base directory if one is set in the CODE variable. It allows me to make a coding tree portable without having to modify all the batch files for each program if I move everything.

提交回复
热议问题