我正在尝试创建一个批处理文件,该文件将在激活和注销系统时倒计时。这适用于用户不再在计算机上的情况。我遇到过多个人们正在这样做的论坛,但只要用户闲置x分钟或几秒钟,他们就会注销。我所做的事情倒计时并注销或取消,我将它设置在任务计划程序中,以便在计算机空闲时启动。那部分工作正常。我似乎无法正常工作的是,如果时间是在下午6:30(18:30)之后才进行倒计时。它曾经为我工作过一次,但我不能让它继续工作。
所以我需要更具体的序列。
下面的内容似乎正好像现在一样完美。
这是我现在的代码,它起作用,它只是不关心它是什么时候。
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
我玩它用不同的时间来测试时间,但它仍然不起作用。
我也试过使用第二个IF语句,但是没有用。
IF !hh! GTR 18 IF !mm! GTR 30 (
goto :begin
)
非常感谢任何帮助!谢谢!
答案 0 :(得分: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
或mm
可能设置为前导“0”,该批次视为八进制,因此08
和09
无效。
在这种情况下,批处理会识别出它无法播放08
或09
,因此会切换到正常(字符串比较)模式并正确比较字符串到18
和30
。
但这是一个陷阱。如果您要将30
替换为7
,那么您会发现08
和09
<{1}}。这是因为当您“切换”到7
模式时(实际上您不切换到数字比较模式,因为一方或另一方无法转换为整数)比较从左侧开始逐个字符进行,string-comparison
小于"0"
它似乎只能起作用,因为你选择的特定数字不会违反这种机制。我称之为失败失败的场景。这是很常见的 - 因为它没有被观察到失败,它被认为是有效的,直到有一天有人改变某些东西并且它会中断......
下一个问题是对"7"
的误解。
if errorlevel
为n 或大于n ,则 IF ERRORLEVEL n
为TRUE。因此errorlevel
总是如此。 IF ERRORLEVEL 0
是对errorlevel = 0的测试。 IF NOT ERRORLEVEL 1
也是如此,除了前者可以在一个块中使用,但后者不能。
您的代码设置为单独测试其他错误级别。如果您要将代码更改为
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
设置为2 ,则转到:end
或大于2
如果您确实使用errorlevel
制作菜单,那么正确的步骤是以反向数字顺序进行测试。这是多余的,因为使用您的代码,按choice
(例如)会将X
设置为25和25&gt; 2,因此errorlevel
2的测试将退出该过程并关闭窗口
再次,失败的情况。
答案 1 :(得分:0)
在摆弄了一点之后,我注意到另一个文件(副本)我已经
:begin
setlocal enableDelayedExpansion
for...
当我仔细查看这两个文件时,我注意到第二个和第三个单词开头的大写字母。我更改了顶部代码setlocal enableextensions enabledelayedexpansion
的代码,我将第一个字母更改为大写setlocal enableExtensions enableDelayedExpansion
,修改了所有内容,现在如果时间尚未下午6:30则取消并退出如果它在6:30之后然后它开始关闭序列并且它工作得很漂亮!谢谢你的任何想法,但我自己回答了! :d
希望这对未来的其他人也有帮助!