0

I don't know how to code a bat file for time comparing.

The bat file is to get current time and if the time now is 00:00 or after but before 7:00, bat file will sent a command to shutdown the computer, else, it will do nothing.

set "currentTime=%TIME%
set flag=false
if %currentTime% geq 07:00 if %currentTime% leq 00:45 set flag=true
if %flag%==true (
    shutdown -s
) else (
    shutdown -a
)

Please help, I'm a new hand, thanks.

1
  • if %time::=% geq 004500.00 if %time::=% leq 070000.00 set flag=true Commented Dec 5, 2015 at 22:52

1 Answer 1

1

your code line

if %currentTime% geq 07:00 if %currentTime% leq 00:45 set flag=true

compares, as a string, the current time to "07:00" and then to "00:45" and if the current time is later than 07:00 AND earlier than 00:45, performs shutdown -s, otherwise perfoms shutdown -a.

Since a time cannot be both, then it will always follow the else path.

But the code is set up to perform shutdown regardless, so it can't "do nothing" as you claim.

Perhaps you want to shutdown before 07:00 but with different paramaters if it's before 00:45

if %currentTime% lss 07:00 (
 if %currentTime% leq 00:45 (
    shutdown -s
  ) else (
    shutdown -a
  )
)

That is shutdown -s before 00:45 and shutdown -a if later than 00:45 but before 07:00

Your next problem is that you don't tell us which tme-formt you use. It's possible to use 24-hour time or 12-hour time and it's possible to use a leading "0" on the hours, or replace that leading 0 with a Space

Without knowing what format you use, there are four possibilities for conversion or comparison. The above should work with 24-hour, leading 0's.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for helping me. 24-hour tme-formt will be utilized and I wanna shutdown the computer after 00:45 but after 07:00. It meas computer will shutdown at 00:45 to 7:00, operate at 7:01 - 00:44.
if %currentTime% leq 07:00 if %currentTime% geq 00:45 shutdown -s should do that - the shutdown parameter is up to you. This should compare the current time to the string 07:00 and if less than or equal to then it compares the time to 00:45 and if it's greater than or equal to that string, executes the shotdown else does nothing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.