Batch-File

批處理文件 IF %time% 不工作

  • January 29, 2021

我正在嘗試製作一個批處理文件來關閉 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)

試試這個:

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  

好吧,你可以這樣做:

: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

引用自:https://serverfault.com/questions/809346