如何解决在特定时间之间执行的批处理文件
我有一个批处理文件,一旦它们能够接收到文件(这是一个变量 - 因为有些可能离线、忙碌或延迟),它就会部署到机器上,但它应该只在当前运行它本地时间在指定窗口内。
例如,仅在上午 12 点至凌晨 2 点之间。
我确实有以下处理 PM 时间的方法 - 但如果我在此处指定任何一位(或两位)数字 AM 小时(例如凌晨 1 点到上午 9 点),显然它不会执行。
@echo off
SET hour=%time:~0,2%
SET shouldrun=True
IF %hour% leq 23 SET shouldrun=False
IF %hour% geq 02 SET shouldrun=False
IF "%shouldrun%"=="False" (
echo Outside Update Schedule
EXIT /B 1
)
IF "%shouldrun%"=="True" (
@TASKKILL /f /im some.exe > nul 2>&1
@timeout /t 4 > nul
- do things here -
@timeout /t 2 > nul
shutdown -r -f -y -t 2
EXIT /B 0
)
解决方法
试试这个:
@echo off & setlocal ENABLEDELAYEDEXPANSION
::takes the current hour independent of the time format
for /f "usebackq tokens=* delims=" %%i in (`wmic os get LocalDateTime /value`) do for /f "tokens=* delims=" %%# in ("%%i") do set "%%#"
set "hour=%LocalDateTime:~6,2%"
::removes the leading zeroes
cmd /c exit /b %hour%
set hour=%errorlevel%
::checks if should run
if !hour! LEQ 23 if !hour! GEQ 2 (
echo Outside Update Schedule
EXIT /B 1
)
echo ---RUNNING----
::your code here. Second if is not needed.
,
如果我为此考虑 wmic,那么我会使用不同的类来做,这样我就不会遇到前导零的问题,也不需要 for 循环。
@%SystemRoot%\System32\wbem\WMIC.exe Path Win32_LocalTime Where "Hour < 2" | %SystemRoot%\System32\findstr.exe "\<0\> \<1\>" 1>NUL 2>&1 || Echo Exit /B 1
@Rem Your line(s) here
,
这似乎工作正常:
@echo off
SET hour=%time:~0,2%
SET shouldrun=True
IF %hour% GEQ 02 IF %hour% LEQ 19 SET shouldrun=False
IF "%shouldrun%"=="False" (
echo Outside Update Schedule
EXIT /B 1
)
IF "%shouldrun%"=="True" (
@TASKKILL /f /im some.exe > nul 2>&1
@timeout /t 4 > nul
- do things here -
@timeout /t 2 > nul
shutdown -r -f -y -t 2
EXIT /B 0
)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。