Computing.Net > Forums > Programming > Detect 'Enter' using SET command?

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Detect 'Enter' using SET command?

Reply to Message Icon

Name: CR
Date: December 10, 2008 at 17:00:25 Pacific
OS: Windows XP Pro SP3
CPU/Ram: AMD XP 2500+ 1GB
Product: Home build / HOME BUILD
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: CR
Date: December 10, 2008 at 17:33:59 Pacific
Reply:

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


0

Response Number 2
Name: CR
Date: December 10, 2008 at 17:51:37 Pacific
Reply:

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 8F

This 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


0

Response Number 3
Name: CR
Date: December 10, 2008 at 18:22:40 Pacific
Reply:

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?


0

Response Number 4
Name: Judago
Date: December 10, 2008 at 22:15:49 Pacific
Reply:

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 8F

If 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


0

Response Number 5
Name: Holla
Date: December 10, 2008 at 22:21:58 Pacific
Reply:

CR,

Try


if "%CMRNAME1%"=="" goto NONAME1

--
Holla.


0

Related Posts

See More



Response Number 6
Name: CR
Date: December 11, 2008 at 03:41:25 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Detect 'Enter' using SET command?

how to use Dos commands in Borlad C www.computing.net/answers/programming/how-to-use-dos-commands-in-borlad-c/4175.html

FOR /L loop using set command www.computing.net/answers/programming/for-l-loop-using-set-command/18705.html

How to use make command www.computing.net/answers/programming/how-to-use-make-command/3765.html