I am trying to create a batch file to check if the environment variables are defined or undefined and gives a certain output statement if it is or not. This is what I have and I can't seem to get the program to tell me that any argument is defined.
if not defined "%MyVar%" (
echo MyVar is NOT defined
)
if defined "%MyVar%" (
echo MyVar IS defined
)
To Check if an Environment Variable ExistsSelect Start > All Programs > Accessories > Command Prompt. In the command window that opens, enter echo %VARIABLE%. Replace VARIABLE with the name of the environment variable. For example, to check if NUKE_DISK_CACHE is set, enter echo %NUKE_DISK_CACHE%.
On the Windows taskbar, right-click the Windows icon and select System. In the Settings window, under Related Settings, click Advanced system settings. On the Advanced tab, click Environment Variables.
To list all the environment variables, use the command " env " (or " printenv "). You could also use " set " to list all the variables, including all local variables.
Environment variables in PowerShell are stored as PS drive (Env: ). To retrieve all the environment variables stored in the OS you can use the below command. You can also use dir env: command to retrieve all environment variables and values.
I think you mean to write:
if not defined MyVar (
echo MyVar is NOT defined
)
and
if defined MyVar (
echo MyVar IS defined
)
This is because cmd.exe will expand the reference to the content of the variable if you enclose it within %
characters. (You want to know if the variable itself is defined, not if a variable with the name of the content of that variable is defined.)
Note that environment variables (names within %
characters) are different from replaceable parameters (%0
, %1
, etc.).
I would recommend switching to Windows PowerShell, because it has built-in parameter parsing features, variable scopes, real functions, and much, much more.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With