| 1. |
Solve : checking a user input is empty? |
|
Answer» Hello, You made 2 mistakesyou 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: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.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:\> |
|