0

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!

4

2 回答 2

1
set hh=!tm:~0,2!
set mm=!tm:~3,2!
if !hh! LEQ 18 if !mm! LEQ 30 (
    exit
)
else goto :begin

语法不正确。“else”关键字必须if-true与操作语句的右括号位于同一行。

正在发生的事情是该else子句将产生一条错误消息,但无论如何批处理都会继续到:begin标签,并且该消息被循环cls内的屏幕从屏幕上清除。for

!var!部分代码不需要语法。它仅在代码块中需要(即带括号的一系列语句。)

当心。hh或者可以用前导“0”设置,因此mm该批次被视为八进制08并且09无效。

在这种情况下,batch 将识别出它不能一致0809因此它会切换到正常(字符串比较)模式并正确地将字符串18and进行比较30

但这是一个陷阱。如果您要替换30为then您会7发现08并且小于. 这是因为当您“切换”到模式时(实际上您不会切换到数字比较模式,因为一侧或另一侧无法转换为整数)比较是从左侧逐个字符执行的,并且小于097string-comparison"0""7"

它似乎只起作用,因为您选择的特定数字不会违反此机制。我称之为失败到失败的场景。这很常见——因为它没有被观察到失败,它被认为是有效的,直到有一天有人改变了一些东西并且它坏了......

下一个问题是对if errorlevel.

IF ERRORLEVEL n如果errorlevel为 n或大于 n则为 TRUE 。IF ERRORLEVEL 0因此总是正确的。IF NOT ERRORLEVEL 1是对 errorlevel=0 的测试。也是如此IF %ERRORLEVEL%==0,只是前者可以在块内使用,而后者不能。

您的代码设置为单独测试其他错误级别。如果您要将代码更改为

if errorlevel 2 echo TWO&goto :end
if errorlevel 3 echo THREE&goto :end

(看 - 为了节省时间,不要打扰提醒)

pause并在您的之前插入exit

:end
pause
exit

然后你会看到产生的消息。按一个字母键,报告将TWO因为if errorlevel 2 echo TWO&goto :end将回显TWO并转到:end如果errorlevel设置为 2或大于 2

如果你真的是choice用来做菜单的,那么正确的做法是倒序测试。这里是多余的,因为使用您的代码,按下X(例如)将设置errorlevel为 25 和 25>2,因此对errorlevel2 的测试将退出过程并关闭窗口。

再次,失败到失败的场景。

于 2015-01-02T16:09:19.397 回答
0

在摆弄了一点之后,我注意到我拥有的另一个文件(副本)

:begin
setlocal enableDelayedExpansion
for...

当我仔细查看这两个文件时,我注意到第二个和第三个单词开头的大写字母。我将其更改为顶部的代码,setlocal enableextensions enabledelayedexpansion并将第一个字母更改为大写setlocal enableExtensions enableDelayedExpansion,它修复了所有内容,现在如果时间还不是下午 6:30,它会取消并退出,如果它在 6:30 之后开始关机顺序,它工作得很好!所以谢谢你的任何想法,但我自己回答了!:D

希望这对将来的其他人也有帮助!

于 2015-01-02T15:14:06.513 回答