首页
学习
活动
专区
圈层
工具
发布

多批Ifs
EN

Stack Overflow用户
提问于 2012-10-02 22:25:38
回答 2查看 658关注 0票数 3

我已经多次尝试让这批产品开始工作了,但它不会起作用,这让我发疯了。在我回答这个问题之前,我也对这个网站进行了梳理,并尝试了很多不同的东西。

我有一个批处理,它将执行关机或重新启动或注销,这取决于您所选择的选项。当我运行这个程序时,问题就出现了,我希望通过检查答案是否为零,将问题从几个小时移到几分钟到几秒。它给出了错误“有一个意外(”),并立即关闭。如果我把它拆开单独运行它就能工作了。

下面是包含If语句的主要函数。如果你想看整批,我可以给你。我已经评论了关闭的实际用途,只是为了帮助解决这个问题。在REM的关闭线之前的2行将被删除,当这被计算出来。

代码语言:javascript
复制
:SR_MAIN
cls
echo+
echo+  
set /p SR_hrs=....Please enter the number of hours before %SR_NAME%:  

IF "%SR_hrs%" == 0 (
     echo+
     echo    You don't want hours, huh?
     timeout /t 2 > nul
     cls
     echo+
     echo+
     set /p ptm=....Please enter the number of minutes before %SR_NAME%:  
     IF %ptm% == 0 (
          echo+
          echo    You don't want minutes, huh?
          timeout /t 2 > nul
          cls
          echo+
          echo+
          set /p pts=....Please enter the number of seconds before %SR_NAME%:  
          IF %pts% == 0 (
               echo+
               echo    Exiting to Shutdown menu...
               timeout /t 2 > nul
               goto SHUTREMENU
          ) ELSE (
               cls
               echo+
               echo+
               echo    This will %SR_NAME% the computer in the seconds you provided.
               set /a tm=pts
               echo+
               echo    Waiting %tm% seconds before continuing...
               echo+
               timeout /t %tm%
                  echo Now would come the %SR_NAME%!
                  pause
REM               shutdown /f /%SR_N% /t 0
               exit
          )
     ) ELSE (
          cls
          echo+
          echo+
          echo    This will %SR_NAME% the computer in the minutes you provided.
          set /a tm=ptm*60
          echo+
          echo    Waiting %ptm% minutes (%tm% seconds) before continuing...
          echo+
          timeout /t %tm%
             echo Now would come the %SR_NAME%!
             pause
REM          shutdown /f /%SR_N% /t 0
          exit
     )
) ELSE (
     cls
     echo+
     echo+
     echo    This will %SR_NAME% the computer in the hours you provided.
     set /a tm=SR_hrs*60*60
     echo+
     echo    Waiting %SR_hrs% hours (%tm% seconds) before continuing...
     echo+
     timeout /t %tm%
        echo Now would come the %SR_NAME%!
        pause
REM     shutdown /f /%SR_N% /t 0
     exit
)

:MAIN_MENU
ECHO Exiting to Main Menu...
PAUSE

谢谢你能提供的任何帮助。这件事对我来说真是个谜。

J

EN

回答 2

Stack Overflow用户

发布于 2012-10-02 23:18:35

有百分之一的信号不见了。

代码语言:javascript
复制
IF "%SR_hrs%" == 0 (

           ↑ there
票数 1
EN

Stack Overflow用户

发布于 2012-10-03 00:35:05

塞巴斯蒂安指出了失踪的%

另一个问题是变量的扩展不能在设置变量的同一个代码块中工作。在执行任何操作之前,都会对整个代码块进行解析,并且像%ptm%这样的代码是在解析时展开的。因此,在设置ptm之前,您将得到它的值!

解决办法是使用延迟扩展。

setlocal enableDelayedExpansion放在脚本的顶部。

然后在代码块中使用!var!而不是%var%

有关延迟扩展的更多信息,请从命令行中键入help set,然后阅读它所述的“最后,支持延迟环境变量展开……”

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12699314

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档