Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
batchcc
- Posts: 139
- Joined: 17 Aug 2015 06:05
-
Contact:
#1
Post
by batchcc » 27 Nov 2015 11:37
I have a batch file that prompts the user to input text but the user can just hit enter or use a space to go on to the next question is there a way to restrict the user from entering space "skip" or just hitting enter? Thanks
Code: Select all
@echo off
Cls
:name
Echo please enter your name
Set /p name=
If %name%==skip goto name
If %name%==" " goto name
That is the beginning of the code and it should work for space and skip but how can I make it work for enter? Is there an easier way to do it that what I have?
-
aGerman
- Expert
- Posts: 4678
- Joined: 22 Jan 2010 18:01
- Location: Germany
#2
Post
by aGerman » 27 Nov 2015 13:19
SET /P reads the input after hitting Enter. Thats the reason why you can only check the entered input afterwards. Predefine variable "name" with a space. If the user pressed Enter without any other input the space will be still saved in the variable. Just read the first character of the name and jump back to the label if it was a space. That way you avoid both no input and input beginnig with a space with only one IF statement.
Code: Select all
@echo off &setlocal
:name
Cls
Set "name= "
Set /p "name=please enter your name: "
if "%name:~,1%"==" " goto name
Echo entered name was "%name%"
Pause
Regards
aGerman