Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 526
Fivos Capone Avatar asked May 06 '16 11:05

Fivos Capone


1 Answers

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

like image 65
Rishav Avatar answered Sep 19 '22 05:09

Rishav