How to pickup null value in variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
phillid
Posts: 109
Joined: 03 Apr 2010 20:27
Location: Wellington, New Zealand
Contact:

How to pickup null value in variable

#1 Post by phillid » 30 Jun 2010 23:51

Hello. I have a program where the user 'inputs' text. The user could type nothing, which gives the error:

Code: Select all

'(' was unexpected at this time

I understand why it does this, but I don't know how to make an IF statement pickup this null value and tell the user to type something.

Thanks

phillid

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to pickup null value in variable

#2 Post by aGerman » 01 Jul 2010 01:03

Have a look at this

Code: Select all

@echo off &setlocal

:: destoy the %input% in case you set any value before
set "input="

:: Wait for user input
set /p "input=Enter your value: "

:: variable input is "not defined" in case the user entered nothing
if not defined input (
  echo you entered nothing
)

:: if you would use quotes, you get no error
if "%input%"=="xyz" (
  echo you entered xyz
) else (
  echo you entered NOT xyz
)

pause



Regards
aGerman

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: How to pickup null value in variable

#3 Post by miskox » 01 Jul 2010 01:08

aGerman always supplies complete answers. Maybe in one line you need this:

Code: Select all

if "%input%"=="" echo Empty string entered.


Hope this helps.

Saso

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to pickup null value in variable

#4 Post by aGerman » 01 Jul 2010 02:41

Yes, that is also a possibility.
Indeed the result of if "%input%"=="" ... is the same like my suggestion if not defined input ...

Regards
aGerman

Post Reply