J Starr
J Starr

Reputation: 33

Multiple Ifs in Batch

I've tried multiple times to get this batch to work but it won't and it's driving me crazy. I've also combed this website and tried many different things before I gave in to asking the question.

I have a batch that will do a shutdown or reboot or logoff depending on what choice you pick. The problem comes in when I run this and want to move from hours to minutes to seconds by checking the answer to see if it's zero. It gives the error "There was an unexpected (" and immediately closes. If I pull this apart and run it individually it works.

Here is the main function that includes the If statement. If you want to look at the entire batch I can give it to you. I've commented out the actual use of shutdown just to help with figuring this out. The 2 lines preceeding the REM'd out shutdown line will be removed when this is figured out.

: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

Thanks for any help you can give. This one has been a real puzzle to me.

J

Upvotes: 3

Views: 772

Answers (2)

dbenham
dbenham

Reputation: 130819

Sebastian pointed out the missing %.

Your other problem is the expansion of variables will not work inside the same code block that sets the variable. The entire code block is parsed before anything is executed, and code like %ptm% is expanded at parse time. So you are getting the value of ptm before it was set!

The solution is to use delayed expansion.

Put setlocal enableDelayedExpansion near the top of your script.

Then use !var! instead of %var% within your blocks of code.

For more info about delayed expansion, type help set from the command line, and read from the point it says "Finally, support for delayed environment variable expansion..."

Upvotes: 1

Sebastian
Sebastian

Reputation: 1889

There's a percent sign missing.

IF "%SR_hrs%" == 0 (

           ↑ there

Upvotes: 1

Related Questions