0

Is there an easier and more logical way to make continuous if - else statements in batch than the one that I am using nowadays. I'm very new to programming etc. and first I tried to make a code like this:

if %time:~0,2% geq 8 (
  if %time:~0,2% leq 9 (
    start "" "C:\users\me\desktop\subject1.bat"
  ) else (
  if %time:~0,2% leq 10 (
    start "" "C:\users\me\desktop\subject2.bat"
  ) else (
    if %time:~0,2% leq 11 (
        start "" "C:\users\me\desktop\subject3.bat"
  )
) else (
Exit
)

Now the idea is that if time isn't over 8 it simply closes the program. But if it is over 8 then it goes and checks if time is less than 9 -> open subject1, less than 10 -> subject2 etc. But the code didn't work on the second else so I started to use this mainly because it works:

if %time:~0,2% geq 8 (
goto :check1
) else (
  Exit
)
:check1 (
  if %time:~0,2% leq 9 (
    start "" "C:\users\me\desktop\subject1.bat"
  ) else (
    goto :check2
  )
)
:check2 (
  if %time:~0,2% leq 10 (
    start "" "C:\users\me\desktop\subject2.bat"
  ) else (
    goto :check3
  )
)
:check3 (
  if %time:~0,2% leq 11 (
    start "" "C:\users\me\desktop\subject3.bat"
  )
)

The second way works but it looks bad and is hard to read even to me. Is there a more simple way to do this? The reason why I made this code is because my laptop is kinda slow to boot (SSHD hybrid drive instead of SSD) so I compensate the lost time by automatically run on boot (done) and connecting to wifi (also done) and opening the needed softwares and websites.

Lolman
  • 107
  • 8

2 Answers2

2

Your first variant: nested if/else blocks

There are two closing parentheses missing in your code (7 × (, 5 × )).

This is your code with a little different indention:

if %time:~0,2% geq 8 (
    if %time:~0,2% leq 9 (
        start "" "C:\users\me\desktop\subject1.bat"
    ) else (
        if %time:~0,2% leq 10 (
            start "" "C:\users\me\desktop\subject2.bat"
        ) else (
            if %time:~0,2% leq 11 (
                start "" "C:\users\me\desktop\subject3.bat"
            )
        ) else (
            exit
        )

What you are trying to accomplish is probably this:

if %time:~0,2% geq 8 (
    if %time:~0,2% leq 9 (
        start "" "C:\users\me\desktop\subject1.bat"
    ) else (
        if %time:~0,2% leq 10 (
            start "" "C:\users\me\desktop\subject2.bat"
        ) else (
            if %time:~0,2% leq 11 (
                start "" "C:\users\me\desktop\subject3.bat"
            )
        )
    )
) else (
    exit
)

You could also write it like this:

if %time:~0,2% geq 8 (
    if %time:~0,2% leq 9 (
        start "" "C:\users\me\desktop\subject1.bat"
    ) else if %time:~0,2% leq 10 (
        start "" "C:\users\me\desktop\subject2.bat"
    ) else if %time:~0,2% leq 11 (
        start "" "C:\users\me\desktop\subject3.bat"
    )
) else (
    exit
)

Your second variant: flat if/else blocks

Also your alternative variant could be simpler; furthermore, labels should not be followed by (:

if not %time:~0,2% geq 8 (
    exit
)
if %time:~0,2% leq 9 (
    start "" "C:\users\me\desktop\subject1.bat"
    goto :cont
)
if %time:~0,2% leq 10 (
    start "" "C:\users\me\desktop\subject2.bat"
    goto :cont
)
if %time:~0,2% leq 11 (
    start "" "C:\users\me\desktop\subject3.bat"
    goto :cont
)
:cont

Additional issues

  1. Consider to use exit /B instead of exit as the former just quites the batch file while the latter terminates the underlying command prompt instance.
  2. start (without the /WAIT option) does not wait until the run batch file has been finished; consider to use the call command instead.
  3. The %time% variable may contain padding zeros, for instance, 08:30, so %time:~0,2% expands to 08 rather than SPACE8; numbers with leading zeros are treated as octal ones, so the comparisons may fail; in order to avoid such trouble, you could do the following before your if/else blocks and use variable %hour% instead of %time:~0,2% for them:

    set "hour=%time:~0,2%" & rem // (extract hour)
    set "hour=1%hour: =0%" & rem // (prepend `1`, replace leading <kbd>SPACE</kbd> by `0`)
    set /A "hour%%=100"    & rem // (remove leading `1`, avoid leading `0` finally)
    
    echo Hour value to be used further: %hour%
    
aschipfl
  • 33,626
  • 12
  • 54
  • 99
0

The Batch code below answer your question: "Is there a more simple way to do this?"

@echo off
setlocal EnableDelayedExpansion

rem Define the "subject" array with batch files for each hour
set "subject[9]=subject1.bat"
set "subject[10]=subject2.bat"
set "subject[11]=subject3.bat"

rem Get the array element corresponding to current hour
set /A hour=1%time:~0,2%-100
set "file=!subject[%hour%]!"

if defined file start "" "C:\users\me\desktop\%file%"

For a further explanation of this solution you must understand what an array is, so I suggest you to read this introduction first, then the Wikipedia article and finally this answer on array management in Batch files.

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108