Batch file 通过批处理文件检查输入
我正在进行用户输入并检查他们是否键入了Batch file 通过批处理文件检查输入,batch-file,Batch File,我正在进行用户输入并检查他们是否键入了n或y。。。它不起作用,因为它说明了两种情况 以下是我所拥有的: @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 :s
n
或y
。。。它不起作用,因为它说明了两种情况
以下是我所拥有的:
@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
首先,您可以在两个if之后添加一个默认goto 然后,在这两个测试中,您必须在%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
需要修改语法吗
这是修改后的代码
@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应该仅为大写字母。为了确保它正常工作,我会做一些不同的事情 首先更正的代码如下,但底部是我建议的代码:
@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%
接下来,我将确保您只有大写字母,因此如果它们是小写的,则进行交换,因为在某些情况下,如果大小写不正确,这实际上不起作用:
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
引号通常在DOS批处理文件中被误用。与unixshell脚本不同,DOS批处理语言并没有被考虑出来。例如,当
isit
变量包含引号时
set isit="Y"
然后,上面的if语句扩展到
IF ""Y"" == "Y" GOTO saidyes
IF ""Y"" == "N" GOTO saidno
因为cmd.exe在计算表达式之前不会删除%isit%”中的引号
isit
中的引号不得与此处发布的原始批处理文件一起出现。但通常在批处理文件中处理路径名,Windows在引号中传递长文件名以隐藏空格。例如,在“C:\ProgramFiles(x86)\Microsoft Visual Studio 10.0\VC”中。为了解决这个问题,通常的做法是编写if语句:
IF ["%isit%"] == ["Y"] GOTO saidyes
IF [%isit%] == ["Y"] GOTO saidyes
IF [%isit%] == [Y] GOTO saidyes
对于设置isit=“Y”
这将变成:
IF [""Y""] == ["Y"] GOTO saidyes
IF ["Y"] == ["Y"] GOTO saidyes
IF ["Y"] == [Y] GOTO saidyes
对于设置isit=Y
:
IF ["Y"] == ["Y"] GOTO saidyes
IF [Y] == ["Y"] GOTO saidyes
IF [Y] == [Y] GOTO saidyes
对于设置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不会计算方括号;他们只是一些很少使用的角色。但这只是个骗局。任何高级批处理脚本都需要取消引用功能:
@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
我稍微更改了代码以帮助解决问题。而不是goto:(函数)你只说goto(函数)
啊。。。对gotos。。。呵呵。谢谢我会在两分钟内做好标记。谢谢我复制粘贴了上面的脚本,得到了与问题相同的问题。我移除了()并且它工作了。也许这不是公认的答案。谢谢Vik,我在发布代码之前没有测试过我的代码(我目前在Linux上),下面是结果。。。我刚刚更正了代码。是的,我对它做了一些修改,但我得到了P+1删除if条件周围的paren使其对我有效。但它没有效果,因为这两种变体都是允许的
@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