开发者

Checking input via a batch file

I am doing user input and checking to see if t开发者_如何学Chey typed n or y... and it's not working because it says both either way.

Here's what I have:

@echo off
set /P theuserinput="Type your name: "
echo So your name is: %theuserinput%?
set /P isit="Y/N: "
echo You typed: %isit%
if (%isit% == "y") goto :saidyes
if (%isit% == "n") goto :saidno

:saidyes
echo Hooray!

:saidno
echo Aww
PAUSE


First you can add a default goto after the two if's.

Then, in both tests you have to add the quotes around %isit% and to remove the parenthesis. You may also add the /I flag to do an insensitive string comparison.

Finally, add goto after each echo to jump over the next one.

@echo off
set /P theuserinput="Type your name: "
echo So your name is: %theuserinput%?
set /P isit="Y/N: "
echo You typed: %isit%
if /I "%isit%" == "Y" goto :saidyes
if /I "%isit%" == "N" goto :saidno
goto :error

:saidyes
echo Hooray!
goto :end

:saidno
echo Aww
goto :end

:error
echo ERROR

:end
PAUSE


Need change in syntax

Here is the modified code

    @echo off
set /P theuserinput="Type your name: "
echo So your name is: %theuserinput%?
set /P isit="Y/N: "
echo You typed: %isit%
if "%isit%" == "Y" GOTO saidyes
if "%isit%" == "N" GOTO saidno

:saidyes
echo Hooray!
GOTO paused

:saidno
echo Aww

:paused
PAUSE

....

In above example The Y/N is supposed to be capital letter only.


There are a few things I would do differently to this to ensure that it works properly...

First off the corrected code is the following, but at the bottom is my suggested code:

@echo off
setlocal
set theuserinput=
set /P theuserinput="Type your name: "
echo So your name is: %theuserinput%?
set /P isit="Y/N: "
echo You typed: %isit%
if '%isit%'== 'Y' goto saidyes
if '%isit%'=='N' goto saidno


:saidyes
echo Hooray!
goto end

:saidno
echo Aww
goto end

:end
pause
endlocal
exit

However, to make sure that you only get Y or N there are a few things I would put in...

First I would make sure that you only grab the first letter by adding:

IF NOT '%isit%'=='' set isit=%isit:~0,1%

Next I would make sure that you only have Capital letters, so you make the swap if they are lowercase, because this will actually not work in some cases if the CaSe isn't correct:

if '%isit%'== 'y' set isit=Y
if '%isit%'== 'Y' goto :saidyes
if '%isit%'=='n' set isit=N 
if '%isit%'=='N' goto :saidno

The final edit for this file could look like the following:

@echo off
:top
set theuserinput=
set /P theuserinput="Type your name: "
echo So your name is: %theuserinput%?
set /P isit="Y/N: "
IF NOT '%isit%'=='' set isit=%isit:~0,1%
IF '%isit%'=='y' set isit=Y
IF '%isit%'=='Y' goto saidyes
IF '%isit%'=='n' set isit=N
IF '%isit%'=='N' goto saidno
goto top

:saidyes
echo You typed: %isit%
echo Hooray!
goto end

:saidno
echo You typed: %isit%
echo Aww
goto end

:end
pause
exit


Quotes are commonly misused in DOS batch files. Unlike UNIX shell scripts, the DOS batch language is not thought-out. For example, when the isit variable contains quotes

set isit="Y"

then the above if-statements expand to

IF ""Y"" == "Y" GOTO saidyes
IF ""Y"" == "N" GOTO saidno

because cmd.exe does not remove the quotes in "%isit%"=="Y" before evaluating the expression.

Quotes in isit shall not occur with the original batch file posted here. But often you process path names in batch files, and Windows passes long file names in quotes to hide blanks. For example, as in "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC". To work around this it is common practice to write if-statements so:

IF ["%isit%"] == ["Y"] GOTO saidyes
IF   [%isit%] == ["Y"] GOTO saidyes
IF   [%isit%] ==   [Y] GOTO saidyes

For set isit="Y" this becomes:

IF [""Y""] == ["Y"] GOTO saidyes
IF   ["Y"] == ["Y"] GOTO saidyes
IF   ["Y"] ==   [Y] GOTO saidyes

and for set isit=Y:

IF ["Y"] == ["Y"] GOTO saidyes
IF   [Y] == ["Y"] GOTO saidyes
IF   [Y] ==   [Y] GOTO saidyes

and for set isit=:

IF [""] == ["Y"] GOTO saidyes
IF   [] == ["Y"] GOTO saidyes
IF   [] ==   [Y] GOTO saidyes

which is far from being elegant, but at least works now for quoted and unquoted variables. It also works when isit is empty. Oftenly batch files stop because of empty variables:

set x=
REM ...
IF %x% == x GOTO x

because now the if-statement expands to:

IF == x GOTO x

and cmd.exe fails. These errors are usually hard to debug.

The trick is very old; I remember using it already in MSDOS. Note that the square brackets are not evaluated by cmd.exe; they're just some rarely used characters. But it's just a trick. Any advanced batch script needs a dequoting-function:

@echo off
 setlocal EnableExtensions
 setlocal EnableDelayedExpansion
 REM ...
 set /P isit="Y/N: "
 call :dequote isit
 REM ...
 IF "!isit!" == "Y" GOTO saidyes
 IF "!isit!" == "N" GOTO saidno
 REM ...
 goto done

:dequote
 for /f "delims=" %%A in ('echo %%%1%%') do set %1=%%~A
 goto :eof

:done

The deqote-subroutine removes any double quotes around the variable name passed to the function.

After dequoting the []-trick is not required anymore. Note also the use of ! instead of %. The exclamation marks force cmd.exe to reexpand isit.


I changed the code somewhat to help fix your problem. Instead of goto :(function) you just say goto (function)

@echo off
set /P theuserinput=Type your name: 
echo So your name is: %theuserinput%?
set /p isit=Y/N: 
echo You typed: %isit%
if %isit% == "y" goto saidyes
if %isit% == "n" goto saidno

:saidyes
echo Hooray!

:saidno
echo Aww
pause
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜