3

I'm trying to make a batch file to shutdown PCs if they're used out of the allowed times. It'll be triggered on boot, but because you can't have 'if and' triggers in Windows Task Scheduler it was necessary to build the time check in to the script.

I've used the following method but it isn't working. Does anybody know why?

IF "%TIME:~0,5%" GEQ "19:58" IF "%TIME:~0,5%" LSS "08:58" (MSG * "Your device has not been authorised for use at this time and will now shutdown." && SHUTDOWN -s -t 120)

Raf
  • 43
  • 2
  • 6

2 Answers2

3

Try this:

SET "ADJUSTEDTIME=%TIME: =0%"
IF "%ADJUSTEDTIME:~0,5%" GEQ "19:58" GOTO :SHUTDOWN  
IF "%ADJUSTEDTIME:~0,5%" LSS "08:58" GOTO :SHUTDOWN  
GOTO :EOF  
:SHUTDOWN  
MSG * "Your device has not been authorised for use at this time and will now shutdown."  
SHUTDOWN -s -t 120  
Greg Askew
  • 34,339
  • 3
  • 52
  • 81
  • That's done it, cheers! What is "GOTO :EOF" for? Are you able to quickly outline why mine didn't work? Don't worry if it'd take a while to explain. – Raf Oct 16 '16 at 14:22
  • EOF is end of file. The script was not handing time where hours were only one digit and it the string had a space in it, so the `"ADJUSTEDTIME=%TIME: =0%"` replaces the space with a zero. Also the IF logic was a bit unclear, so it is easier to understand if you separate the tests for the time. – Greg Askew Oct 16 '16 at 14:29
0

Well, you can do:

:a
for /f "tokens=*" %%i in ('time /t') DO set time=%i
if %time% == "7:58" (shutdown -r -t 59 -c "You can not use at this time")
goto :a

The for command sets the time variable to the current time, the if command checks if it is 19:58