Search code examples
batch-fileoptimizationshutdown

I'm writing a small program to shutdown a computer at a certain time. Somethings wrong though


My code is below. It was supposed to be a simple code to check for the time, and compare it to two different times where I want the child's computer off for the night. I have an extremely basic understanding of coding since I have not really done anything with coding that was very extreme. I thought I would give this a go, but I'm not entirely sure what I have done wrong in the code, I am able to go through the different steps until I reach the if statement where it is: if %yesno% EQU y (.... and anything after that, it says, for some reason that the if statement was not expected at this time and then closes the window straight away. I was able to get a very quick screen shot off. I based all of my code on multiple google searches on different parts of the code. If anyone can help out, that would be very useful.

(screenshot for reference) https://i.gyazo.com/1449d3d48279b64411d982d27ec0940a.png

@echo off

:questions
cls
set /p start=[Shutdown time (The hour of shutdown, do not add minutes):]
set /p 1ampm=[am/pm:] 
cls
set /p end=[Enter the time you want the computer to be available again for use(The hour of shutdown, do not add minutes):]
set /p 2ampm=[am/pm:]
cls
echo Loading....
PING 1.1.1.1 -n 1 -w 2000 >NUL
cls

set /p yesno=[The time you selected the computer to remain off is from %start% - %end% , is this correct, y/n?]
cls
goto :yn

:yn
if %yesno% EQU y (
    goto :1timecorrect
) if %yesno% EQU n (
    goto :questions
) else (
    goto :questions
)

:1timecorrect
if %1ampm% EQU am (
    goto :2timecorrect
) if %1ampm% EQU pm (
    set realstart=%start%+12
    goto :2timecorrect
) else (
    cls
    echo you did not enter whether or not the start time is am or pm
    goto :questions
)
:2timecorrect
if %2ampm% EQU am (
    goto :Begining
) if %2ampm% EQU pm (
    set realend=%end%+12 
    goto :Begining
) else (
    cls
    echo you did not enter whether or not the start time is am or pm
    goto :questions
)

:Begining
set mytime=%time:~0,2%

:Start
if %mytime% GEQ %realstart% (
    cls
    echo time has expired, time to go to bed.
    shutdown -s -f -t 60 -c "Your computer is about to be shut down in 1 minute"  
) else (
    if %mytime% LEQ %realend% (
        echo time has expired, time to go to bed.
        shutdown -s -f -t 60 -c "Your computer is about to be shut down in 1 minute"
    ) else (
        cls
        echo This program is Opperating correctly
        PING 1.1.1.1 -n 1 -w 600000 >NUL
        goto :Start
    )  
)

Solution

  • Since everything under if %mytime% GEQ ~start should only be run if mytime is higher than start, everything under that line should be in parentheses. Also, if your if statement has an else, the first closing parenthesis, else, and last opening parenthesis need to b on the same line.

    @echo off
    
    set start=11
    set end=19
    
    set mytime=%time:~0,2%
    
    :Start
    if %mytime% GEQ %start% (
        if %mytime% LEQ %end% (
            shutdown -s -f -t 60 -c "Your computer is about to be shut down in 1 minute"
        ) else (  
            echo This program is Operating correctly
            sleep 1800
            goto :Start
        )
    )