I am trying to create a batch file that will countdown when activated and logoff of the system. This is for when the user is no longer at the computer. I have come across multiple forums where people are doing this but they log off any time the user is idle for x minutes or seconds. What I have does count down and does log off or cancel and I have it set in the task scheduler to start when the computer has been idle. That part is working fine. What I cannot seem to get to work right is to only perform the countdown if the time is after 6:30 PM (18:30). It has worked once for me but I cannot get it to work anymore.
So what I need in a more specific sequence.
- Needs to check the time to make sure it is after 6:30 pm
- Needs to exit if time is not yet 6:30 pm without user interaction.
- begin log off sequence if time is after 6:30 pm
The stuff below seems to be working perfectly as of right now.
- press any letter key to cancel the sequence
- force log off if the sequence is not canceled
Here is my code right now which works, it just doesn't care about what time it is.
TITLE Automatic Log Off Initiated!
mode con:cols=80 lines=1
COLOR CF
@echo off
setlocal enableextensions enabledelayedexpansion
set tm=%time%
:: Test data on lines below.
:: set tm=18:59:59.00
:: set tm=19:00:00.00
:: set tm=19:44:59.00
:: set tm=19:45:00.00
:: set tm=23:59:59.99
set hh=!tm:~0,2!
set mm=!tm:~3,2!
if !hh! LEQ 18 if !mm! LEQ 30 (
exit
)
else goto :begin
:begin
for /l %%N in (600 -1 1) do (
set /a "min=%%N/60, sec=%%N%%60, n-=1"
if !sec! lss 10 set sec=0!sec!
cls
choice /c:1ABCDEFGHIJKLMNOPQRSTUVWXYZ /n /m "Automatically Logging Off in !min!:!sec! - Press any key or move the mouse to cancel" /t 1 /d 1
if not errorlevel 1 goto :break
if errorlevel 2 goto :end
if errorlevel 3 goto :end
if errorlevel 4 goto :end
if errorlevel 5 goto :end
if errorlevel 6 goto :end
if errorlevel 7 goto :end
if errorlevel 8 goto :end
if errorlevel 9 goto :end
if errorlevel 10 goto :end
if errorlevel 11 goto :end
if errorlevel 12 goto :end
if errorlevel 13 goto :end
if errorlevel 14 goto :end
if errorlevel 15 goto :end
if errorlevel 16 goto :end
if errorlevel 17 goto :end
if errorlevel 18 goto :end
if errorlevel 19 goto :end
if errorlevel 20 goto :end
if errorlevel 21 goto :end
if errorlevel 22 goto :end
if errorlevel 23 goto :end
if errorlevel 24 goto :end
if errorlevel 25 goto :end
if errorlevel 26 goto :end
if errorlevel 27 goto :end
)
:break
shutdown /l /f
:end
exit
I have played with it to test the time using different times but it still does not work.
I have also tried using a second IF statement but that did not work.
IF !hh! GTR 18 IF !mm! GTR 30 (
goto :begin
)
Any help is greatly appreciated! Thank you!