50

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?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Fivos Capone
  • 523
  • 1
  • 5
  • 9
  • 3
    What about `if defined calltwo`? – aschipfl May 06 '16 at 11:43
  • 3
    your `if` _is_ working, but the variable is never empty - you successfully built an endless loop and finally will get an errormessage (recursion depth overflow). – Stephan May 06 '16 at 11:49
  • There is a crucial difference in your need between "test if is set (exists)" and "test if the value is not empty" – Sandburg Oct 09 '19 at 11:13

4 Answers4

57
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

Rishav
  • 3,818
  • 1
  • 31
  • 49
  • 2
    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. – SomethingDark Mar 22 '18 at 08:54
  • 2
    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. – Rishav Mar 22 '18 at 08:56
  • 4
    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). – SomethingDark Mar 22 '18 at 08:58
  • May I use a pattern of variable names? So if I know it starts with `ICPP...` Can I use something like `ICPP*`? – Royi Oct 07 '19 at 19:41
  • 6
    `IF "%wefgejroigejrmghuiohrbtg%"=="" ECHO Variable is NOT defined` does not output anything for me on Windows 10 20H2. Obviously, that variable is not defined. – bers Dec 15 '20 at 08:35
  • 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)` – johny why Nov 23 '21 at 22:38
  • 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. – gluttony Mar 09 '22 at 11:08
48

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?

Community
  • 1
  • 1
K4dse
  • 481
  • 3
  • 4
5

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

Philip Kelley
  • 39,426
  • 11
  • 57
  • 92
  • 2
    gives you an Syntax error, when the variable happens to have a space. Only secure way is using quotes `IF "%Variable%"==""` (like accepted answer) – Stephan Mar 23 '18 at 21:35
  • 1
    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. – Mofi Mar 24 '18 at 15:31
5

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
Matthew Howard
  • 159
  • 1
  • 3