I'm writing a batch command script where in there is a check for Environment variables. I need to write a FOR loop by passing all the required variables and then validate if it is defined or not and if it is not defined then prompt the value for that key and set that variable permanently.
Problem is that I could not dereference the loop variable and check it in Environment variable.
Sample code is as below:
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%G IN (JBOSS_HOME, JAVA_HOME, ANT_HOME, PERFORCE_PATH, P4CLIENT) DO (
ECHO.
ECHO. Loop Item : %%G
:: Call a function by sending each value to check if it is set in ENVIRONMENT Variables, if not then add it persistently.
CALL:validateAndUpdate %%G
)
GOTO:EOF
:validateAndUpdate
:: Now I have to check if passed value is available in ENVIRONMENT variable or not
:: echo %~1 will print value like JBOSS_HOME
:: Below IF condition always substitues to IF ("JBOSS_HOME" == []) and it always returns true
:: but could not find syntax to use it to DE-reference and check if that key is set.
:: Ex: IF %JBOSS_HOME% == []
IF ("%~1" == []) (
ECHO.
ECHO. %~1 is empty
SET /p value="Enter value for '%~1' : "
:: Set that key value pair persistently using SETX
SETX %~1 "%value%"
) ELSE (
ECHO.
ECHO. %~1 is available as ENVIRONMENT variable
)
GOTO:EOF
ENDLOCAL