1.

Solve : checking a user input is empty?

Answer»

Hello,
I have written this small batch file to ask the username
It should not allow him to go further if he dont provide username.
I have written this to do the same.
but its not working.
could you please let me know where i made the mistake.

echo off
:uname
SET /P username=Please enter your username:

IF %username% == "" GOTO uname
echo entered usernameYou made 2 mistakes

1. in 3rd line there should be quotes around %username%
2. in 4th  line there should be percent signs around username

Like this

echo off
:uname
SET /P username=Please enter your username:
IF "%username%"=="" GOTO uname
echo entered %username%I have modified the bat file to correct the two mistakes.
Still it is not prompting me if i dont provide any input. i am just typing enter button.
Then bat file execution is getting completed. But it should prompt me to enter username.
I am using Windows 2000.

echo off
:uname
SET /P username=Please enter your username:

IF "%username%" == "" GOTO uname
echo entered %username%

Quote from: Dias de verano on NOVEMBER 06, 2008, 12:18:46 AM

You made 2 mistakes

1. in 3rd line there should be quotes around %username%
2. in 4th  line there should be percent signs around username

Like this

echo off
:uname
SET /P username=Please enter your username:
IF "%username%"=="" GOTO uname
echo entered %username%
you need to reset the %username% vaule otherwise it'll remember what you entered last time:

Code: [Select]set username=
set /p username=Please enter username:

FBHello,
Still same problem.
Have you checked it in your machine.
thanks in advance.

Quote from: fireballs on November 06, 2008, 01:22:15 AM
you need to reset the %username% vaule otherwise it'll remember what you entered last time:

Code: [Select]set username=
set /p username=Please enter username:

FB
run your script without the Code: [Select] echo off at the beginning and the problem will most likely be obvious.

FBThe username variable is defined by the system and is unlikely ever to be blank.

Try using another variable name:

Code: [Select]echo off
setlocal
:uname
SET /P uname=Please enter your username:

IF .%uname%==. GOTO uname
echo entered %uname%
endlocal

 thank you very much for your response.
Its working perfectly.
But could you please tell what does the . used in if condition, setlocal convays?
i am beginner to DOS.
thanks in advance.

Quote from: Sidewinder on November 06, 2008, 04:18:22 AM
The username variable is defined by the system and is unlikely ever to be blank.

Try using another variable name:

Code: [Select]echo off
setlocal
:uname
SET /P uname=Please enter your username:

IF .%uname%==. GOTO uname
echo entered %uname%
endlocal

 
Quote
But could you please tell what does the . used in if condition

The dot keeps the if condition balanced. If uname is blank, the if condition compares dot to dot. There is no WAY to directly compare nulls in batch code. Note: the dot is arbitrary, you can use any character; I PREFER dots because they are lower case.

Quote
setlocal convays

setlocal and it's evil twin endlocal, RESTRICT the life of variables. In this case uname starts blank with each execution of your code.

  Quote from: Sidewinder
I prefer dots because they are lower case.

Don't understand what you mean. Dots are neither lower or upper case. Only the letters of the alphabet have "case".
Possibly he means you do not have to hold shift to get them?
But, I am also unsure of what he means.Anyway, there are lots of ways to get around the "comparing nulls" problem.


quotes are most common I think

if "%variable1%"=="%variable2%"

square brackets

if [%variable1%]==[%variable2%]

Or a single dot...

I think using quotes as a rule makes sense, as it also takes care of the chance a variable might contain spaces. Quote from: gregory on November 09, 2008, 02:45:41 AM
I think using quotes as a rule makes sense, as it also takes care of the chance a variable might contain spaces.

Indeed.

Code: [Select]S:\>set var=hello world

S:\>echo %var%
hello world

S:\>if %var%==hello world echo Yes
world==hello was unexpected at this time.

S:\>if {%var%}=={hello world} echo Yes
world}=={hello was unexpected at this time.

S:\>if "%var%"=="hello world" echo Yes
Yes

S:\>


Discussion

No Comment Found