How to check if a variable exists in a batch file?
Solution 1
IF "%Variable%"=="" ECHO Variable is NOT defined
This should help but this works, provided the value of Variable does not contain double quotes. Or you may try. Both worked for me.
VERIFY OTHER 2>nul
SETLOCAL ENABLEEXTENSIONS
IF ERRORLEVEL 1 ECHO Unable to enable extensions
IF DEFINED MyVar (ECHO MyVar IS defined) ELSE (ECHO MyVar is NOT defined)
ENDLOCAL
Solution 2
The easiest way is just using the command line extension DEFINED. This is also my preferred way of doing this.
in your case:
@echo off
set calltwo=%1
echo %calltwo%
if defined calltwo (
echo Error
)else (
call beingcalled.bat randomnumber
)
If this doesn't work for you there is a workaround in the link below.
The question is also a duplicate of: Check if an environment variable is defined without command extensions and without using a batch file?
Solution 3
This is just a follow-up to the comment (and bounty) post by @Rishav
Here’s a trick I picked up a very long time ago:
@ECHO OFF
SET Foo=%1
ECHO == Start ====================
ECHO %Foo%
IF %Foo%x == x ECHO Parameter not set
ECHO == End ====================
ECHO.
If the parameter is not set, you get a check of x==x
If the parameter is set (to, say, “asdf”), you get a check of asdfx==x
Solution 4
None of these solutions work for me on windows 10, but I did find a solution that does work
IF %foo%==^%foo^% ECHO variable not defined
Fivos Capone
Updated on July 13, 2022Comments
-
Fivos Capone almost 2 years
I am using the
call
command:call beingcalled.bat randomnumber
In beingcalled.bat:
@echo off set call=%1 echo %call% set call=%call%%call% call caller.bat %call%`
In caller.bat:
@echo off set calltwo=%1 echo %calltwo% if "%calltwo%"== "" ( echo Error ) else ( call beingcalled.bat randomnumber )
Why does the command
if "%calltwo%"== ""
not work? And how do I see if a variable was set? -
SomethingDark over 6 yearsWhy did you open a bounty for a question that you have the accepted answer to? If you think your answer could be improved, just update it.
-
Rishav over 6 yearsI opened the bounty hoping someone would give a better answer. My answer although works Im searching for something more efficient and simply better answer.
-
SomethingDark over 6 yearsThe
if defined
answer that both you and K4dse posted is the method that is considered best practice (although the first three lines of your second answer are unnecessary). -
Stephan over 6 yearsgives you an Syntax error, when the variable happens to have a space. Only secure way is using quotes
IF "%Variable%"==""
(like accepted answer) -
Mofi over 6 yearsEven
IF "%Variable%"==""
is not secure and can result in a exit of batch processing because of a syntax error or doing something complete different than batch file is designed for depending on value ofVariable
. Example:Variable
has the value" == "" call dir %USERPROFILE% & pause & rem "
. Secure is only enabling delayed expansion and usingIF "!Variable!" == ""
as in this case the value of environment variableVariable
does not modify the IF command line finally executed bycmd.exe
after preprocessing it. -
Royi over 4 yearsMay I use a pattern of variable names? So if I know it starts with
ICPP...
Can I use something likeICPP*
? -
bers over 3 years
IF "%wefgejroigejrmghuiohrbtg%"=="" ECHO Variable is NOT defined
does not output anything for me on Windows 10 20H2. Obviously, that variable is not defined. -
johny why over 2 yearsDoes this work with command-line arguments? On Win 10, this returns "NOT defined", even if arg 3 was passed to the script:
IF DEFINED %3 (ECHO defined) ELSE (ECHO NOT defined)
-
gluttony over 2 yearsComparison does not work for me too on Windows 10, and I see when I do
echo %NO_VAR_WITH_THIS_NAME%
it echoes%NO_VAR_WITH_THIS_NAME%
, not an empty string. Howeverif defined
does properly the job.