批处理中的多个Ifs

时间:2012-10-02 22:25:38

标签: if-statement batch-file

我已多次尝试让这批产品上班,但它不会让我疯狂。在我提出问题之前,我还梳理了这个网站并尝试了很多不同的东西。

我有一个批次可以执行关闭或重启或注销,具体取决于您选择的选项。当我运行这个问题并且想要通过检查答案以确定它是否为零时,需要从几小时到几分钟移动到几秒钟。它给出了错误"出现意外("并立即关闭。如果我把它拆开并单独运行它就可以了。

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

: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

感谢您提供任何帮助。这个对我来说是一个真正的难题。

Ĵ

2 个答案:

答案 0 :(得分:1)

缺少百分号。

IF "%SR_hrs%" == 0 (

           ↑ there

答案 1 :(得分:1)

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

你的另一个问题是变量的扩展在设置变量的同一代码块内不起作用。在执行任何操作之前解析整个代码块,并在解析时扩展像%ptm%这样的代码。因此,在设置之前,您将获得ptm的值!

解决方案是使用延迟扩展。

setlocal enableDelayedExpansion放在脚本顶部附近。

然后在您的代码块中使用!var!代替%var%

有关延迟扩展的详细信息,请从命令行键入help set,然后从“最后支持延迟环境变量扩展...

相关问题