通过批处理文件检查输入

时间:2011-04-14 16:26:34

标签: batch-file

我正在进行用户输入并检查他们是否输入了ny ...并且它无法正常工作,因为它无论哪种方式都有效。

以下是我所拥有的:

@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

5 个答案:

答案 0 :(得分:4)

首先,您可以在两个if之后添加默认转到。

然后,在两个测试中,您必须在%isit%周围添加引号并删除括号。您还可以添加/ I标志以进行不敏感的字符串比较。

最后,在每个回声之后添加goto以跳过下一个。

@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

答案 1 :(得分:2)

需要更改语法

这是修改后的代码

    @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

...

在上面的例子中,Y / N应该只是大写字母。

答案 2 :(得分:2)

我会做一些不同的事情,以确保它正常工作......

首先关闭更正的代码如下,但底部是我建议的代码:

@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

然而,为了确保你只得到Y或N,我会提出一些事情......

首先,我要确保你只通过添加:

来抓住第一个字母
IF NOT '%isit%'=='' set isit=%isit:~0,1%

接下来我会确保你只有大写字母,所以如果它们是小写的话你就进行交换,因为如果CaSe不正确,这在某些情况下实际上不起作用:

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

此文件的最终编辑内容如下所示:

@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

答案 3 :(得分:2)

引用通常在DOS批处理文件中被滥用。与UNIX shell脚本不同,DOS批处理语言不经过深思熟虑。例如,isit变量包含引号

set isit="Y"

然后上面的if语句扩展为

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

因为 cmd.exe 在评估表达式之前未删除"%isit%"=="Y"中的引号。

此处发布的原始批处理文件不会出现isit中的引号。但是,您经常在批处理文件中处理路径名,Windows会在引号中传递长文件名以隐藏空格。例如,如“C:\ Program Files(x86)\ Microsoft Visual Studio 10.0 \ VC”。要解决这个问题,通常的做法是编写if语句:

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

对于set isit="Y",这变为:

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

set isit=Y

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

set isit=

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

这远非优雅,但至少现在适用于引用和未引用的变量。它在isit为空时也有效。由于空变量,批处理文件通常会停止:

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

因为现在if语句扩展为:

IF == x GOTO x

cmd.exe 失败。这些错误通常很难调试。

诀窍很老;我记得在MSDOS中已经使用过它。请注意, cmd.exe 不评估方括号;他们只是一些很少使用的角色。但这只是一个技巧。任何高级批处理脚本都需要一个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

deqote - 子例程删除传递给函数的变量名称周围的任何双引号。

在取消引用之后[] - 不再需要技巧了。另请注意使用!代替%。感叹号强制 cmd.exe 重新展开isit

答案 4 :(得分:0)

我稍微更改了代码以帮助解决您的问题。而不是goto :(函数)你只是说goto(函数)

@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