Batch file 我的批处理文件无法工作我不断得到的变量是意外的

Batch file 我的批处理文件无法工作我不断得到的变量是意外的,batch-file,Batch File,你在这些方面有一些问题 cls title The Dead May Follow echo. echo hello echo. echo what is your name. echo. set /p name= echo. echo hello %name% im Nathan. echo. echo i wake up in bed after finding out the night before your mother has died. echo. echo do i get d

你在这些方面有一些问题

cls
title The Dead May Follow
echo.
echo hello 
echo.
echo what is your name.
echo.
set /p name=
echo.
echo hello %name% im Nathan.
echo.
echo i wake up in bed after finding out the night before your mother has died.
echo.
echo do i get dressed and go downstairs or do i sleep in longer.
echo.
set /p i=
echo.
if %i% equ get dressed and go downstairs goto get dressed and go downstairs 
if %i% equ sleep longer goto sleep longer
if %i% neq sleep longer goto start
if %i% neq get dressed and go downstairs goto start
:get dressed and go downstairs
cls
echo.
echo your the only one home 
echo.
pause
:sleep longer
cls
echo.
echo you wake up its 11 o clock and nobody is home
pause
exit
如果%i%eq get,则解析为一个条件,如果此条件为true,则执行一个命令:dressed和一些参数下楼goto get dressed并下楼

如果必须在字符串中使用空格,请使用

if %i% equ get dressed and go downstairs goto get dressed and go downstairs 
好的,解决了这个问题。下一个问题:goto需要一个单词标签,没有空格。这段代码将得到并忽略行的其余部分:一个标签只需要前11个字符;其余的都被忽略了

因此,上面的一行应该是:

if "%i%" equ "get dressed and go downstairs" goto get dressed and go downstairs 

您可以使用if/i使您的条件不区分大小写Get DresSed=Get DresSed

批处理文件具有以下建议的更改:

并查看Microsoft的文档,其中有一句重要的话:

goto命令仅使用标签的前八个字符

标签中允许有空格,但不应使用空格。最好对标签使用CamelCase符号,同时考虑到只有前8个字符是有效的


这部分文档对于Windows 2000及更高版本的Windows不再适用。Windows 95/98也是如此。但是,使用提供的示例进行的一个简单测试表明,在Windows XP上,超过8个字符的标签是重要的。

对我来说,它工作得很好,也许您可以告诉我们您在做什么,以及您是如何得到这个错误的。您需要告诉我们您提供了哪些实体来显示错误,以及错误是什么。最突出的是您使用的if%i%。。。如果。。。如果您没有提供任何输入。将其更改为if/i%i%eq which…-/i引入了大小写不敏感,引号确保空字符串或包含空格的字符串本身的行为。请注意,例如,sleep longer是一个无效的标签,因为它包含一个空格,batch在操作符的每一侧只坚持一个字符串。睡眠时间延长是一条线。睡眠时间延长两个小时。
if "%i%" equ "get dressed and go downstairs" goto getDressed  
@echo off
:Restart
cls
title The Dead May Follow
echo.
echo hello 
echo.
echo What is your name?
echo.
set /p "name="
echo.
echo Hello %name% im Nathan.
echo.
echo I wake up in bed after finding out the night before your mother has died.
echo.
echo Do I get dressed and go downstairs or do I sleep in longer?
echo.
set /p "i="
echo.
if /i "%i%" equ "get dressed and go downstairs" goto GetDressed
if /i "%i%" equ "sleep longer" goto SleepLonger
goto Restart
:GetDressed
cls
echo.
echo Your the only one home.
echo.
pause
:SleepLonger
cls
echo.
echo You wake up. It's 11 o clock and nobody is at home.
pause
exit