Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi all, I am asking a question using the SET command within a batch file using Windows XP Pro SP3 and need to know how to detect if nothing is entered - for example, if the user simply presses the Enter/Return key.
This is what I have at the moment:
SET /P CMRNAME1= Please enter your name?
This correctly records what ever the user types in and stores it in the var CMRNAME1. However, if a user simply presses the Enter/Return key the batch exits.... I need it to enter/set a default value such as 'No Name'.
Can this be done with something like:
IF /P CMRNAME1="" SET/P CMRNAME1= No Name
Please help as this has been bugging me for a few days now and no ammount of searching either here or on Google has produced a solution.

I found out how to do it... This is what I have:
SET /P CMRNAME1= Please enter your name?
IF %CMRNAME1%'==' SET CMRNAME1=No Name
goto NONAME
ECHO Stored. && %R1%
goto end
:NONAME
COLOR 4F
ECHO Nothing entered... && %R1%
:end
COLOR 8F%R1% is simply a 1 second pause using the following: SET R1=Start /w Commands/Rest1.vbs
The contents of the Rest1.vbs file are as follows: Wscript.sleep 875

Hmmm, my bad - still doesn't work correctly... It now seems to detect when I press Enter/Return and skips on by. However, when I DO enter something it still skips...
I now have the following:
:NONAME1
CLS
SET /P CMRNAME1= Please enter your Name?
IF %CMRNAME1%'==' goto NONAME1
COLOR 2F
ECHO Stored. && %R1%
COLOR 8FThis work around does stop users pressing the Enter/Return key without first entering the correct information and to be fair is better than storing a 'misc' var of xxxx as before.
Don't you just love it when you answer your own questions :P

GRR - now it would seem that I cannot enter any spaces. For example if I enter the CMRNAME as 'Joe' then it works fine. However, If I enter the CMRNAME as 'Joe Bloggs' then the batch file quits....
So, my final question is how can I have the above function (IE Enter/Return does nothing but start the loop again) but still be able to insert more than one word... Anyone?

I think the far better option is to check if the cmrname variable is defined, this way what ever it contains doesn't bother us, we just know something is inside. Also note "set cmrname=" this is to make sure the variable is undefined before we start, this can be especially important if the script goes back to ask for new input later because set /p doesn't clear a variable if you only press enter, instead it leaves it's contents intact. Another important thing to note is that the variable name in the if not defined statement doesn't have markers, e.g % or !, if you put markers in the contents of the variable is tested not the variable itself.
:NONAME1
CLS
set CMRNAME1=
SET /P CMRNAME1= Please enter your Name?
IF not defined CMRNAME1 goto NONAME1
COLOR 2F
ECHO Stored. && %R1%
COLOR 8FIf doesn't like spaces thrown all about the place, if you had of used "double quotes" your if statement probably would have worked. The pitfall of using this method is that if the variable contains quotes more problems start up....
IF "%CMRNAME1%"=="" goto NONAME1

Thanks both for your replies, I ended up going for Judago's solution which worked just as planned :) Thanks for that.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |