Skip to content

Instantly share code, notes, and snippets.

@davidruhmann
Created December 7, 2012 17:00
Show Gist options
  • Save davidruhmann/4234677 to your computer and use it in GitHub Desktop.
Save davidruhmann/4234677 to your computer and use it in GitHub Desktop.
[Batch] Detect if a variable name is defined with a value.
:: Determine if a Variable name is defined with a value.
:: by David Ruhmann
:: Hide Commands
@echo off
setlocal ENABLEEXTENSIONS
:: Example Usage
set "xVar=Value"
call :IsDefined xResult xVar
if "%xResult%"=="true" echo.Defined
set "xVar="
call :IsDefined xResult xVar
if not "%xResult%"=="true" echo.Undefined
pause
goto End
:End
endlocal
:: by David Ruhmann
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:IsDefined <xResult> <xInput>
:: Verify if a variable is defined, return true if defined.
:::: Made as an alternative to DEFINED, both methods require ENABLEEXTENSIONS.
if not "%~1"=="" set "%~1=false"
for /f "usebackq tokens=1,2* delims==" %%a in (`set`) do if "%~2"=="%%~a" if not "%~1"=="" set "%~1=true"
goto :eof
:: by David Ruhmann
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment