Page 1 of 1
How to pickup null value in variable
Posted: 30 Jun 2010 23:51
by phillid
Hello. I have a program where the user 'inputs' text. The user could type nothing, which gives the error:
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
Re: How to pickup null value in variable
Posted: 01 Jul 2010 01:03
by aGerman
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
Re: How to pickup null value in variable
Posted: 01 Jul 2010 01:08
by miskox
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
Re: How to pickup null value in variable
Posted: 01 Jul 2010 02:41
by aGerman
Yes, that is also a possibility.
Indeed the result of if "%input%"=="" ... is the same like my suggestion if not defined input ...
Regards
aGerman