How to check if a variable exists in a batch file?

88,197

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

source http://www.robvanderwoude.com/battech_defined.php

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
Share:
88,197
Fivos Capone
Author by

Fivos Capone

Updated on July 13, 2022

Comments

  • Fivos Capone
    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
    SomethingDark over 6 years
    Why 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
    Rishav over 6 years
    I 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
    SomethingDark over 6 years
    The 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
    Stephan over 6 years
    gives you an Syntax error, when the variable happens to have a space. Only secure way is using quotes IF "%Variable%"=="" (like accepted answer)
  • Mofi
    Mofi over 6 years
    Even 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 of Variable. Example: Variable has the value " == "" call dir %USERPROFILE% & pause & rem ". Secure is only enabling delayed expansion and using IF "!Variable!" == "" as in this case the value of environment variable Variable does not modify the IF command line finally executed by cmd.exe after preprocessing it.
  • Royi
    Royi over 4 years
    May I use a pattern of variable names? So if I know it starts with ICPP... Can I use something like ICPP*?
  • bers
    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
    johny why over 2 years
    Does 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
    gluttony over 2 years
    Comparison 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. However if defined does properly the job.