[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[文本处理] 批处理如何判断ini文本在最近1.5小时内是否被更新过

本帖最后由 linlianboy 于 2022-8-1 09:04 编辑

D盘下有个auto目录内的ini每隔一段时间就会更新下里面的内容,由于程序经常会出现卡死,请问下各位如何通过bat判断ini文本在1.5小时内是否被更新过,如果没有更新过就重启电脑,有更新就循环继续判断

大佬根据1.5小时内未更新就重启写了代码,目前我发现这个
D盘下auto目录内的config_del.ini这个文本,里面的内容未变动,最后修改的时间也是实时刷新的.

请问下有什么办法实现1.5小时内判断config_del.ini这个文本里面的内容是否有变动,如果里面的内容没有出现变动就执行重启,出现变动就继续循环检测

本帖最后由 flashercs 于 2022-7-29 22:28 编辑
  1. <#*,:&cls
  2. @echo off
  3. cd /d "%~dp0"
  4. powershell -C "Set-Location -LiteralPath ([Environment]::CurrentDirectory);. ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~f0\" -ReadCount 0 | Out-String)))"
  5. exit /b
  6. #>
  7. # 超时90分钟
  8. $span0 = New-TimeSpan -Minutes 90
  9. while ($true) {
  10.   $timeOfDay = (Get-Date).TimeOfDay
  11.   if ($timeOfDay -ge '00:00:00' -and $timeOfDay -lt '23:00:00') {
  12.     if ((Get-Item -LiteralPath .\1.ini | New-TimeSpan) -gt $span0) {
  13.       '超时1.5小时'
  14.       $(
  15.         & "C:\ldplayer\Ldconsole.exe" quitall
  16.         choice /t 2 /d y /n
  17.         taskkill /f /im my.exe
  18.         taskkill /f/ im dnmultiplayer.exe
  19.         taskkill /f /im LdVBoxHeadless.exe
  20.         taskkill /f /im LdVBoxSVC.exe
  21.         shutdown.exe /r /f /t 0
  22.       )>$null 2>$null
  23.       break
  24.     } else {
  25.       '未超时'
  26.     }
  27.   }
  28.   Start-Sleep -Seconds 10
  29. }
复制代码
微信:flashercs
QQ:49908356

TOP

  1. :loop
  2. for /f "delims=" %%i in ('dir /b *.ini') do set t=%%~ti
  3. set t=%t:/=%
  4. set t=%t: =%
  5. set t=%t::=%
  6. ping -n 5400 127.1 >nul
  7. for /f "delims=" %%i in ('dir /b *.ini') do set tt=%%~ti
  8. set tt=%tt:/=%
  9. set tt=%tt: =%
  10. set tt=%tt::=%
  11. if %tt% gtr %t% (goto :loop) else (reboot)
复制代码

TOP

回复 3# idwma


    请问能不能限制这个代码在每天的00:00-23:00之间生效

TOP

回复 2# flashercs


        请问能不能限制这个代码在每天的00:00-23:00之间生效

TOP

回复 5# linlianboy


    2楼代码已修改
微信:flashercs
QQ:49908356

TOP

回复 6# flashercs


    非常感谢您

TOP

本帖最后由 linlianboy 于 2022-7-29 13:16 编辑

回复 2# flashercs


    您好.您的这个段代码里面的重启能在增加以下条件吗,先taskkill结束以下进程后再进行重启,因为直接重启有些程序进程内没结束就一直卡死不会执行重启
请问能把以下命令添加到重启前面吗
C:\ldplayer\Ldconsole.exe quitall>nul 2>nul
choice /t 2 /d y /n>nul 2>nul
taskkill /f /im my.exe>nul 2>nul
taskkill /f/ im dnmultiplayer.exe>nul 2>nul
taskkill /f /im LdVBoxHeadless.exe>nul 2>nul
taskkill /f /im LdVBoxSVC.exe>nul 2>nul
shutdown -r -f -t 00>nul 2>nul

TOP

  1.     <#*,:&cls
  2.     @echo off
  3.     cd /d "%~dp0"
  4. echo C:\ldplayer\Ldconsole.exe quitall^>nul 2^>nul>test.bat
  5. echo choice /t 2 /d y /n^>nul 2^>nul>>test.bat
  6. echo taskkill /f /im my.exe^>nul 2^>nul>>test.bat
  7. echo taskkill /f/ im dnmultiplayer.exe^>nul 2^>nul>>test.bat
  8. echo taskkill /f /im LdVBoxHeadless.exe^>nul 2^>nul>>test.bat
  9. echo taskkill /f /im LdVBoxSVC.exe^>nul 2^>nul>>test.bat
  10. echo exit>>test.bat
  11.     powershell -C "Set-Location -LiteralPath ([Environment]::CurrentDirectory);. ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~f0\" -ReadCount 0 | Out-String)))"
  12.     exit /b
  13.     #>
  14.     # 超时90分钟
  15.     $span0 = New-TimeSpan -Minutes 90
  16.     while ($true) {
  17.       $timeOfDay = (Get-Date).TimeOfDay
  18.       if ($timeOfDay -ge '00:00:00' -and $timeOfDay -lt '23:00:00') {
  19.         if ((Get-Item -LiteralPath .\1.ini | New-TimeSpan) -gt $span0) {
  20.           '超时1.5小时'
  21.           start test.bat
  22.           shutdown.exe /r /t 0
  23.           break
  24.         } else {
  25.           '未超时'
  26.         }
  27.       }
  28.       Start-Sleep -Seconds 10
  29.     }
复制代码
https://pc.woozooo.com/mydisk.php

TOP

回复 9# LJY4.0


    非常感激您

TOP

回复 2# flashercs


大佬根据1.5小时内未更新就重启写了代码,目前我发现这个
D盘下auto目录内的config_del.ini这个文本,里面的内容未变动,最后修改的时间也是实时刷新的.

请问下有什么办法实现1.5小时内判断config_del.ini这个文本里面的内容是否有变动,如果里面的内容没有出现变动就执行重启,出现变动就继续循环检测

TOP

本帖最后由 flashercs 于 2022-8-1 15:46 编辑

回复 11# linlianboy
  1. <#*,:&cls
  2. @echo off
  3. cd /d "%~dp0"
  4. powershell -C "Set-Location -LiteralPath ([Environment]::CurrentDirectory);. ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~f0\" -ReadCount 0 | Out-String)))"
  5. exit /b
  6. #>
  7. # 超时90分钟
  8. $span0 = New-TimeSpan -Minutes 90
  9. # ini file
  10. $inifile = ".\1.ini"
  11. function Get-DataHash {
  12.   [CmdletBinding(DefaultParameterSetName = "Path")]
  13.   [OutputType('Microsoft.Powershell.Utility.FileHash')]
  14.   param(
  15.     [Parameter(Mandatory = $true, ParameterSetName = "Path", ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Position = 0)]
  16.     [System.String[]]
  17.     $Path,
  18.     [Parameter(Mandatory = $true, ParameterSetName = "LiteralPath", ValueFromPipelineByPropertyName = $true)]
  19.     [ValidateNotNull()]
  20.     [Alias("PSPath")]
  21.     [System.String[]]
  22.     $LiteralPath,
  23.     [Parameter(Mandatory = $true, ParameterSetName = "Stream", ValueFromPipeline = $true, Position = 0)]
  24.     [System.IO.Stream]
  25.     $InputStream,
  26.     [Parameter(Mandatory = $true, ParameterSetName = "Bytes", Position = 0)]
  27.     [byte[]]
  28.     $Buffer,
  29.     [Parameter(Mandatory = $false, ParameterSetName = "Bytes")]
  30.     [int]
  31.     $Offset = -1,
  32.     [Parameter(Mandatory = $false, ParameterSetName = "Bytes")]
  33.     [int]
  34.     $Count = -1,
  35.     [Parameter(Mandatory = $false)]
  36.     [ValidateSet("SHA1", "SHA256", "SHA384", "SHA512", "MD5", "RIPEMD160")]
  37.     [System.String]
  38.     $Algorithm = "SHA256",
  39.     # Consider all input data as a single data.
  40.     [switch]$AllTogether,
  41.     # return Hash as byte[]
  42.     [ValidateSet("Hex", "Base64", "Bytes")]
  43.     [string]$HashMode = "Hex"
  44.   )
  45.   begin {
  46.     # Construct the strongly-typed crypto object
  47.     [System.Security.Cryptography.HashAlgorithm]$hashAlg = [System.Security.Cryptography.HashAlgorithm]::Create($Algorithm)
  48.     $Algorithm = $Algorithm.ToUpperInvariant()
  49.     if ($AllTogether) {
  50.       $cryptoStream = New-Object System.Security.Cryptography.CryptoStream -ArgumentList ([System.IO.Stream]::Null, $hashAlg, 'Write')
  51.     }
  52.     function GetPso {
  53.       param (
  54.         [byte[]]$Hash,
  55.         [string]$RelatedPath
  56.       )
  57.       ($pso = 0 | Select-Object -Property @{
  58.           Name       = "Algorithm"
  59.           Expression = { $Algorithm }
  60.         }, @{
  61.           Name       = "Hash"
  62.           Expression = {
  63.             switch ($HashMode) {
  64.               "Hex" { [System.BitConverter]::ToString($Hash) -replace "-" }
  65.               "Base64" { [convert]::ToBase64String($Hash) }
  66.               "Bytes" { $Hash }
  67.             }
  68.           }
  69.         }, @{
  70.           Name       = "Path"
  71.           Expression = { $RelatedPath }
  72.         }) | Add-Member -TypeName Microsoft.Powershell.Utility.FileHash
  73.       $pso
  74.       # New-Object psobject -Property ([ordered]@{
  75.       #     Algorithm = $Algorithm
  76.       #     Hash      = [System.BitConverter]::ToString($Hash) -replace "-", ""
  77.       #     Path      = $RelatedPath
  78.       #   })
  79.     }
  80.   }
  81.   process {
  82.     switch ($PSCmdlet.ParameterSetName) {
  83.       "Stream" {
  84.         if ($AllTogether) {
  85.           $InputStream.CopyTo($cryptoStream)
  86.         } else {
  87.           GetPso -Hash ($hashAlg.ComputeHash($InputStream))
  88.         }
  89.         continue
  90.       }
  91.       "Bytes" {
  92.         if ($AllTogether) {
  93.           if ($Offset -lt 0 -or $Count -lt 0) {
  94.             $cryptoStream.Write($Buffer, 0, $Buffer.Count)
  95.           } else {
  96.             $cryptoStream.Write($Buffer, $Offset, $Count)
  97.           }
  98.         } else {
  99.           if ($Offset -lt 0 -or $Count -lt 0) {
  100.             $hash = $hashAlg.ComputeHash($Buffer)
  101.           } else {
  102.             $hash = $hashAlg.ComputeHash($Buffer, $Offset, $Count)
  103.           }
  104.           GetPso -Hash $hash
  105.         }
  106.         continue
  107.       }
  108.       "Path" {
  109.         $pathsToProcess = Convert-Path -Path $Path
  110.       }
  111.       "LiteralPath" {
  112.         $pathsToProcess = Convert-Path -LiteralPath $LiteralPath
  113.       }
  114.       { $true } {
  115.         foreach ($filePath in $pathsToProcess) {
  116.           if (Test-Path -LiteralPath $filePath -PathType Container) {
  117.             continue
  118.           }
  119.           try {
  120.             # Read the file specified in $FilePath as a Byte array
  121.             $stream = [System.IO.File]::OpenRead($filePath)
  122.             if ($AllTogether) {
  123.               $stream.CopyTo($cryptoStream)
  124.             } else {
  125.               GetPso -Hash ($hashAlg.ComputeHash($stream)) -RelatedPath $filePath
  126.             }
  127.           } catch {
  128.             # $PSCmdlet.WriteError($_)
  129.           } finally {
  130.             if ($null -ne $stream) {
  131.               $stream.Dispose()
  132.             }
  133.           }
  134.         }
  135.         break
  136.       }
  137.     }
  138.   }
  139.   end {
  140.     if ($AllTogether) {
  141.       $cryptoStream.Clear()
  142.       $cryptoStream.Dispose()
  143.       GetPso -Hash ($hashAlg.Hash)
  144.     }
  145.     $hashAlg.Clear()
  146.     $hashAlg.Dispose()
  147.   }
  148. }
  149. # main
  150. $pso = New-Object psobject
  151. $pso | Add-Member -MemberType NoteProperty -Name UpdateTime -Value (Get-Date)
  152. $pso | Add-Member -MemberType NoteProperty -Name CurrentTime -Value (Get-Date)
  153. $pso | Add-Member -MemberType ScriptProperty -Name TimeElapsed -Value { $this.CurrentTime - $this.UpdateTime }
  154. $pso | Add-Member -MemberType NoteProperty -Name FileHash -Value $null
  155. while ($true) {
  156.   $pso.CurrentTime = Get-Date
  157.   $timeOfDay = $pso.CurrentTime.TimeOfDay
  158.   if ($timeOfDay -ge '00:00:00' -and $timeOfDay -lt '23:00:00') {
  159.     $oHash = Get-DataHash -LiteralPath $inifile -Algorithm SHA256
  160.     if ($pso.FileHash -ne $oHash.Hash) {
  161.       $pso.FileHash = $oHash.Hash
  162.       $pso.UpdateTime = $pso.CurrentTime
  163.     }
  164.     $pso
  165.     if ($pso.TimeElapsed -gt $span0) {
  166.       '超时1.5小时'
  167.       $(
  168.         & "C:\ldplayer\Ldconsole.exe" quitall
  169.         choice /t 2 /d y /n
  170.         taskkill /f /im my.exe
  171.         taskkill /f/ im dnmultiplayer.exe
  172.         taskkill /f /im LdVBoxHeadless.exe
  173.         taskkill /f /im LdVBoxSVC.exe
  174.         shutdown.exe /r /f /t 0
  175.       )>$null 2>$null
  176.       break
  177.     } else {
  178.       '未超时'
  179.     }
  180.   }
  181.   Start-Sleep -Seconds 10
  182. }
复制代码
微信:flashercs
QQ:49908356

TOP

回复 12# flashercs


    哇塞,代码量这么大的吗,大佬您头像的支付宝是收款码吗.我给你发个红包

TOP

返回列表