我的批处理代码跳过if语句

时间:2017-05-27 17:53:50

标签: batch-file if-statement

注意:这不是重复,因为我发现的其他问题与我的目标不同,并且是不同的if statement

我想创建一个可以记住你名字的批处理文件,即使在关闭之后也是如此。它将名称保存在文本文件中,然后从中读取。我的问题是它跳过我的第一个if statement。为什么这样做?当我输入y时,它会转到:no。我认为这是因为if statement之后是:no部分,但我做错了什么?如果您不明白这一点,只需复制此代码并粘贴到记事本中,然后另存为ANYTHING.bat并运行它,您就会明白我的意思。

无论如何,这是代码:

@echo off
title The logger
echo NAME> log1.txt
set /p name=<log1.txt

:start
cls
echo Your name is %name%, right? (Y/N)
set /p input=
if %input%=="Y" goto yes
if %input%=="y" goto yes
if %input%=="N" goto no
if %input%=="n" goto no

:no
echo Oh... then what is it?
set /p name=
echo OK! Thanks, %name%
echo %name%>log1.txt
pause
goto start

:yes
echo hahahaha i knew it
echo Would you like to change it? (Y/N)
set /p input=

if %input%=="Y" goto namechange
if %input%=="y" goto namechange
if %input%=="N" (
    echo Ok.
    pause
    goto start
)
if %input%=="n" (
    echo Ok.
    pause
    goto start
)
goto start

:namechange
echo What should it be?
set /p name=
echo %name%>log1.txt
echo Thanks, %name%
pause
goto start

3 个答案:

答案 0 :(得分:0)

IF语法为if string1 operator string2 action

如果string1string2被“引用”(允许字符串包含空格),则必须引用 BOTH 字符串。

答案 1 :(得分:0)

我对您的代码进行了一些非常小的更改,使其运行得非常完美,并且对名字进行了更准确的猜测。

@echo off
title The logger
echo %username%> log1.txt
set /p name=<log1.txt
set remove=0123456789
set string=%name%
for /F "tokens=1,2 delims=%remove%" %%a in ("%string%") do (
echo %%a%%b > log1.txt
)
set /p name=<log1.txt


:start
cls
echo Your name is %name%, right? (Y/N)
set INPUT=
set /P INPUT=Type input: %=%
if "%input%"=="Y" goto yes
if "%input%"=="y" goto yes
if "%input%"=="N" goto no
if "%input%"=="n" goto no

:no
echo Oh... then what is it?
set /p name=
echo OK! Thanks, %name%
echo %name%>log1.txt
pause
goto start

:yes
echo hahahaha i knew it
echo Would you like to change it? (Y/N)
set /p input=

if "%input%"=="Y" goto namechange
if "%input%"=="y" goto namechange
if "%input%"=="N" (
    echo Ok.
    pause
    goto start
)
if "%input%"=="n" (
    echo Ok.
    pause
    goto start
)
goto start

:namechange
echo What should it be?
set /p name=
echo %name%>log1.txt
echo Thanks, %name%
pause
goto start

答案 2 :(得分:0)

你所要做的就是从Y&amp; amp;周围带走“引号”。 Ñ