allow a space in user input without quotes

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
batchcc
Posts: 139
Joined: 17 Aug 2015 06:05
Contact:

allow a space in user input without quotes

#1 Post by batchcc » 19 Oct 2015 16:51

Hi I have a batch file that contains a list of possible words the user can type in and the file will "echo" a different word but the user must type in one or two words and the first word will always be "to"if there are two words for example to run or the use could input a single word Like jump. Thanks sorry for any typos auto correct is correcting things that it shouldn't

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: allow a space in user input without quotes

#2 Post by Squashman » 19 Oct 2015 16:57

How about you read this again.
viewtopic.php?f=3&t=6108

Then come back and supply more information.

batchcc
Posts: 139
Joined: 17 Aug 2015 06:05
Contact:

Re: allow a space in user input without quotes

#3 Post by batchcc » 20 Oct 2015 14:06

OK I have a batch file named file.bat

Code: Select all

@echo off
Cls
Set /p action
If %action%== jump echo jump
If %action%== to jump echo jumping

Is there a way to allow the user to enter to jump without adding quotes ?

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: allow a space in user input without quotes

#4 Post by ShadowThief » 20 Oct 2015 14:16

Just put quotes around the check.

Code: Select all

@echo off
Cls
Set /p action=
If "%action%"=="jump" echo jump
If "%action%"=="to jump" echo jumping
pause

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: allow a space in user input without quotes

#5 Post by penpen » 20 Oct 2015 14:32

Without adding quotes:

Code: Select all

@echo off
Cls
Set /p "action="
If %action: =^ % == jump echo jump
If %action: =^ % == to^ jump echo jumping


penpen

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: allow a space in user input without quotes

#6 Post by Squashman » 20 Oct 2015 16:38

penpen wrote:Without adding quotes:

Code: Select all

@echo off
Cls
Set /p "action="
If %action: =^ % == jump echo jump
If %action: =^ % == to^ jump echo jumping


penpen

I hope the SO police don't see that code! :lol:

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: allow a space in user input without quotes

#7 Post by penpen » 20 Oct 2015 18:36

In such a case i would have edited my post and explained the code - i don't want to mess with the official SO police :roll: (i am misusing the quote-block for that):
penpen wrote:You only must escape the space characters with a '^' (tilde) in order to make it work within the expression of the "if-statement".
Here is a simple example that demonstrates the use of this special character and solves the task (just remove the last line):

Code: Select all

@echo off
Cls
Set /p "action="
If %action: =^ % == jump call ^
:jumping "1"
If %action: =^ % == to^
 jump ^
echo^
:jumping
if "%~1" == "1" echo jump & goto ^
:eof
echo I get up, and nothing gets me down.


penpen

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: allow a space in user input without quotes

#8 Post by dbenham » 21 Oct 2015 05:31

Or use FOR /F to make the code generic:

Code: Select all

@echo off
setlocal
cls
set /p "action="
for /f "tokens=1,2" %%A in ("%action%") do (
  if "%%B" equ "" (echo(%%A) else if /i "%%A" equ "to" (echo %%Bing) else echo ERROR
)


Dave Benham

Post Reply