# Solved: Checking for null values in dos



## potchi (May 12, 2011)

Hi, how can i check for null values in dos?

my code here is not working

if "%myParameter1%"==" " echo Please set myParameter1
exit /b

if "%myParameter2%"==" " echo Please set myParameter2
exit /b

....

I want to put an error message if the variable is null before it partially exit (exit /b)

THANKs!


----------



## Squashman (Apr 4, 2003)

I bet if you read the the help on the *IF* command you will see how to check for *undefined* variables.


----------



## centauricw (Jun 26, 2010)

You will want to use the extended command DEFINED for IF. This will test if the environment variable has been set before you attempt to use it. For this, you'll need to be sure ENABLEEXTENSIONS is on.


```
SETLOCAL ENABLEEXTENSIONS

IF NOT DEFINED "%myParameter1% (
    ECHO Please set myParameter1
    EXIT /B
)
```
Note that you can group commands in the IF block by enclosing them in parenthesis. To get the help on any command line function or utility, just enter *COMMAND /?* and it will show you the details of how the command is used, though you may have try it a few times to get the syntax right. If you intend to write a lot of advanced (i.e. not trivial) command scripts, I recommend the books _Windows NT Shell Scripting_ (although this book is old, it is still very relevant to command-line programming) and _Windows Command-Line Administrator's Pocket Consultant_.


----------



## potchi (May 12, 2011)

Hi!

centauricw: the code is not working, it doesn't check for null values but good thing that it can handle multiple lines of commands

squashman: help IF commands?

Thanks!


----------



## Squashman (Apr 4, 2003)

Trying to teach people to fish here but the code above is correct.

Potchi, you can read the help for any command by typing the command name followed by a forward slash and question mark at the cmd line.


```
H:\>echo /?
Displays messages, or turns command-echoing on or off.

  ECHO [ON | OFF]
  ECHO [message]

Type ECHO without parameters to display the current echo setting.

H:\>
```
The code above does work.

```
H:\>set myparm=

H:\>IF defined %myparm% echo Your variable is defined

H:\>If not defined %myparm% echo Your variable has nothing assigned to it
Your variable has nothing assigned to it

H:\>
```


----------



## centauricw (Jun 26, 2010)

For environment variables, there is no such thing as a NULL value. If an environment variable is set to any value, it is defined. If it has no value, the variable not not exist in the environment and is not defined. For example, SET by itself only returns defined variables.

If you are talking about empty strings (which some call null strings), then the code you posted is incorrect. You have a single space between the quotes. This is not an empty string. Also, the extended commands only work on Windows NT and higher. No Windows 95/98/ME or DOS.



> Trying to teach people to fish here


Yeah, Squashman, it's not working.


----------



## potchi (May 12, 2011)

Ah, I see, yeah it working in batch, but actually my variable comes in a properties file. (myFile.properties)

First is to read the .properties file and get the parameters (maxHeap and initialHeap)

So in my potchi.bat I have this code

For /F "tokens=1* delims==" %%A IN (myFile.properties) DO (
IF "%%A"=="maxHeap" set maxHeap=%%B
IF "%%A"=="initialHeap" set initialHeap=%%B
)

In addition to my potchi.bat code, I want to check if maxHeap and initialHeap is set, so what I did in my potchi.bat is

@echo off
For /F "tokens=1* delims==" %%A IN (myFile.properties) DO (
IF "%%A"=="maxHeap" set maxHeap=%%B
IF "%%A"=="initialHeap" set initialHeap=%%B
)

SETLOCAL ENABLEEXTENSIONS

IF NOT DEFINED "%maxHeap%" (
ECHO Please set maxHeap in myFile.properties
Exit /B
)

IF NOT DEFINED "%initialHeap%"(
ECHO Please set initialHeap in myFile.properties
Exit /B
)

Now in myFile.properties i have this line

initialHeap=512
maxHeap=

and when I run potchi.bat

Yeah its good, it echo
Please set maxHeap in myFile.properties

But doing another test,
that is, in myFile.properties I have this line

initialHeap=
maxHeap=2048

Running potchi.bat result to
Please set maxHeap in myFile.properties

expected output:
Please set initialHeap in myFile.properties

Also, even both parameters is set in myFile.properties
initialHeap=512
maxHeap=2048

it still echo
Please set maxHeap in myFile.properties


----------



## Squashman (Apr 4, 2003)

Potchi, please try and use CODE tags around your scripts so we know where the start and stop of your script is.

This code works. I am not sure why your code is not. It looks correct to me.

```
@Echo Off & SetLocal EnableExtensions
For /F "tokens=1* delims==" %%A IN (myFile.properties) DO (
	IF "%%A"=="maxHeap" set maxHeap=%%B
	IF "%%A"=="initialHeap" set initialHeap=%%B
)

echo.%maxHeap%
echo.%initialHeap%

IF NOT DEFINED maxHeap ECHO Please set maxHeap in myFile.properties &Goto :EOF
IF NOT DEFINED initialHeap ECHO Please set initialHeap in myFile.properties &Goto :EOF
echo.Both variables are defined
```
Output from all 3 instances.

```
E:\batch files\potchi>potchi.bat
2048
512
Both variables are defined

E:\batch files\potchi>potchi.bat

512
Please set maxHeap in myFile.properties

E:\batch files\potchi>potchi.bat
2048

Please set initialHeap in myFile.properties

E:\batch files\potchi>
```


----------



## potchi (May 12, 2011)

Thanks squashman that code works!


----------

