批处理文件 IF %time% 不起作用

批处理文件 IF %time% 不起作用

我正在尝试制作一个批处理文件,用于在超出允许使用时间时关闭 PC。它会在启动时触发,但由于 Windows 任务计划程序中不能有“if and”触发器,因此必须在脚本中构建时间检查。

我使用了以下方法,但不起作用。有人知道原因吗?

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)

答案1

尝试这个:

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  

答案2

那么你可以这样做:

: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

for 命令将时间变量设置为当前时间,if 命令检查是否为 19:58

相关内容