用于检查时间的批处理脚本

问题描述 投票:0回答:1

是否可以检查时间是否在两个范围之间,然后根据该时间执行操作。

例如:仅当当前时间不是在11:00 PM和6.00 AM之间执行操作。

到目前为止,这里是我所能提供的,但是我无法固定在中间。

set "currentTime=!TIME::=!" 
set "currentTime=!currentTime:,=!"
set "_bit_compare=leq"
set "compareTime=2300"
set "compareTime1=600"

(time /t | findstr /l "AM PM" || set "_bit_compare=gtr")>nul

if "!currentTime:~0,4=!" %_bit_compare% "!compareTime!" (

    do somethinf

)
batch-file cmd command-line command
1个回答
0
投票

使用PowerShell相对容易编写。如果您在受支持的Windows系统上,则可以使用PowerShell。这也克服了cmd.exe中区域变异的众多问题。

$h = [int](Get-Date -Format "HH")
if ($h -lt 9 -or $h -gt 23) { & notepad.exe }"

要在.bat文件脚本中使用它,您可以:

powershell -NoLogo -NoProfile -Command ^
    "$h = [int](Get-Date -Format "HH")" ^
    "if ($h -lt 9 -or $h -gt 23) { & notepad.exe }"
© www.soinside.com 2019 - 2024. All rights reserved.