简体   繁体   English

将当前时间与 Windows 批处理文件中上次修改的文件日期时间进行比较

[英]Compare current time to file date time last modified in Windows Batch File

I would like a command in a batch file to only execute if it has not been ran for the last 15 minutes.我希望批处理文件中的命令仅在过去 15 分钟内未运行时才执行。 To achieve this I can create a "last_run" file to log the time last ran.为此,我可以创建一个“last_run”文件来记录上次运行的时间。

Where I am stuck is comparing the time last modified to the current time and taking action if the time last modified.我被卡住的地方是将上次修改时间与当前时间进行比较,并在上次修改时间时采取行动。

Here is my current code:这是我当前的代码:

@echo off
set filename=last_run.txt
if not exist %filename% (
   rem NOT EXISTS, CREATE FILE THEN PRINT THE LIST
   echo.>"%filename%"
   GOTO PrintList
) else (
   rem FILE EXISTS, GET TIME LAST MODIFIED
   FOR %%? IN (%filename%) DO (set filetime=%%~t?)
   echo %filetime%
   rem IF TIME LAST MODIFIED > 15 MINS PRINT THE LIST
   if timediff??(%filetime%, current) > 15 mins {
      GOTO PrintList
   } else {
      GOTO End
   }
)

:PrintList
rem PRINT THE LIST
echo now print the list
rem WRITE TO THE FILE TO UPDATE TIME LAST MODIFIED
echo.>"%filename%"
GOTO End

:End

Besides the missing time calculation, your code has a delayed expansion problem .除了缺少时间计算之外,您的代码还有延迟扩展问题 (My solution below doesn't need delayed expansion) (我下面的解决方案不需要延迟扩展)

cmd is incredibly crude when it comes to date/time calculation (it can be done, but...). cmd在日期/时间计算方面非常粗糙(它可以完成,但是......)。

Better use the help of another language (like PowerShell):更好地使用另一种语言(如 PowerShell)的帮助:

@echo off
setlocal 
set "filename=last_run.txt"
set "minutes=15"
if not exist "%filename%" (
   break>"%filename%"
   goto :PrintList
)
for /f %%a in ('"powershell Test-Path %filename% -OlderThan (Get-Date).AddMinutes(-%minutes%)"') do set "older=%%a"
if "%older%" == "True" (
  echo %filename% is older than %minutes% minutes; print list
  break>"%filename%"
  goto :PrintList
) else (
  echo %filename% is younger than %minutes% minutes; exiting
  goto :eof
)
:PrintList
echo insert your payload here.

You could use total minutes with a powerhell command.您可以使用 powerhell 命令使用总分钟数。

@echo off
set error=0
if not exist last_run.txt echo File not yet existed, first run will be now & set /a error+=1
    for /f "delims=," %%i in ('powershell -command "(New-TimeSpan -Start (Get-Date "01/01/1970") -End (Get-Date)).TotalMinutes"') do set "now=%%i"
    if "%error%" == "1" goto :run
    for /f %%a in (last_run.txt) do set earlier=%%a
    set /a result=%now%-%earlier% 
    if %result% geq 15 (
        :run
           echo RUN COMMANDS HERE
           echo %now%>tmp.tmp
    )

The concept is simple.这个概念很简单。 We get the epoch time in minutes.我们以分钟为单位获得纪元时间。 Store the value in a file.将值存储在文件中。 Then compare the current minutes with the minutes in the file.然后将当前分钟数与文件中的分钟数进行比较。 if %now% is equal to or more than 15 from %earlier% the command will run.如果%now%等于或大于%earlier%15 ,则命令将运行。 Additionally, if the file does not yet exist, it will create it and run the command first time.此外,如果该文件尚不存在,它将创建它并第一次运行该命令。 From there it will only run if the seconds in the file is 15 or less than current minutes.从那里它只会​​在文件中的秒数为 15 或小于当前分钟时运行。

Another implementation using from your :使用另一种实现:

@Echo Off
Set "filename=last_run.txt"
Set "minutes=15"
If Not Exist "%filename%" GoTo :PrintList
"%__APPDIR__%WindowsPowerShell\v1.0\powershell.exe" -NoProfile If(((Get-Date)-(Get-Item ".\%filename%").LastWriteTime).Minutes -LT %minutes%){Exit 1}
If ErrorLevel 1 Exit /B
Rem Your payload below here.

:PrintList
CD.>"%filename%"

If you are happy to accept that the PC running this code will always have the appropriate entries in %PATH% and %PATHEXT% , you can change "%__APPDIR__%WindowsPowerShell\\v1.0\\powershell.exe" to just PowerShell .如果您愿意接受运行此代码的 PC 将始终在%PATH%%PATHEXT%具有适当的条目,您可以将"%__APPDIR__%WindowsPowerShell\\v1.0\\powershell.exe"更改为PowerShell

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM