标题: [文本处理] 批处理如何判断ini文本在最近1.5小时内是否被更新过 [打印本页]
作者: linlianboy 时间: 2022-7-12 18:27 标题: 批处理如何判断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-12 19:54
本帖最后由 flashercs 于 2022-7-29 22:28 编辑
- <#*,:&cls
- @echo off
- cd /d "%~dp0"
- powershell -C "Set-Location -LiteralPath ([Environment]::CurrentDirectory);. ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~f0\" -ReadCount 0 | Out-String)))"
-
- exit /b
- #>
- # 超时90分钟
- $span0 = New-TimeSpan -Minutes 90
- while ($true) {
- $timeOfDay = (Get-Date).TimeOfDay
- if ($timeOfDay -ge '00:00:00' -and $timeOfDay -lt '23:00:00') {
- if ((Get-Item -LiteralPath .\1.ini | New-TimeSpan) -gt $span0) {
- '超时1.5小时'
- $(
- & "C:\ldplayer\Ldconsole.exe" quitall
- choice /t 2 /d y /n
- taskkill /f /im my.exe
- taskkill /f/ im dnmultiplayer.exe
- taskkill /f /im LdVBoxHeadless.exe
- taskkill /f /im LdVBoxSVC.exe
- shutdown.exe /r /f /t 0
- )>$null 2>$null
- break
- } else {
- '未超时'
- }
- }
- Start-Sleep -Seconds 10
- }
复制代码
作者: idwma 时间: 2022-7-12 19:55
- :loop
- for /f "delims=" %%i in ('dir /b *.ini') do set t=%%~ti
- set t=%t:/=%
- set t=%t: =%
- set t=%t::=%
- ping -n 5400 127.1 >nul
- for /f "delims=" %%i in ('dir /b *.ini') do set tt=%%~ti
- set tt=%tt:/=%
- set tt=%tt: =%
- set tt=%tt::=%
- if %tt% gtr %t% (goto :loop) else (reboot)
复制代码
作者: linlianboy 时间: 2022-7-13 10:46
回复 3# idwma
请问能不能限制这个代码在每天的00:00-23:00之间生效
作者: linlianboy 时间: 2022-7-13 10:46
回复 2# flashercs
请问能不能限制这个代码在每天的00:00-23:00之间生效
作者: flashercs 时间: 2022-7-13 11:07
回复 5# linlianboy
2楼代码已修改
作者: linlianboy 时间: 2022-7-13 13:01
回复 6# flashercs
非常感谢您
作者: linlianboy 时间: 2022-7-29 13:14
本帖最后由 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
作者: LJY4.0 时间: 2022-7-29 17:23
- <#*,:&cls
- @echo off
- cd /d "%~dp0"
- echo C:\ldplayer\Ldconsole.exe quitall^>nul 2^>nul>test.bat
- echo choice /t 2 /d y /n^>nul 2^>nul>>test.bat
- echo taskkill /f /im my.exe^>nul 2^>nul>>test.bat
- echo taskkill /f/ im dnmultiplayer.exe^>nul 2^>nul>>test.bat
- echo taskkill /f /im LdVBoxHeadless.exe^>nul 2^>nul>>test.bat
- echo taskkill /f /im LdVBoxSVC.exe^>nul 2^>nul>>test.bat
- echo exit>>test.bat
- powershell -C "Set-Location -LiteralPath ([Environment]::CurrentDirectory);. ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~f0\" -ReadCount 0 | Out-String)))"
- exit /b
- #>
- # 超时90分钟
- $span0 = New-TimeSpan -Minutes 90
- while ($true) {
- $timeOfDay = (Get-Date).TimeOfDay
- if ($timeOfDay -ge '00:00:00' -and $timeOfDay -lt '23:00:00') {
- if ((Get-Item -LiteralPath .\1.ini | New-TimeSpan) -gt $span0) {
- '超时1.5小时'
- start test.bat
- shutdown.exe /r /t 0
- break
- } else {
- '未超时'
- }
- }
- Start-Sleep -Seconds 10
- }
复制代码
作者: linlianboy 时间: 2022-7-29 20:25
回复 9# LJY4.0
非常感激您
作者: linlianboy 时间: 2022-8-1 09:04
回复 2# flashercs
大佬根据1.5小时内未更新就重启写了代码,目前我发现这个
D盘下auto目录内的config_del.ini这个文本,里面的内容未变动,最后修改的时间也是实时刷新的.
请问下有什么办法实现1.5小时内判断config_del.ini这个文本里面的内容是否有变动,如果里面的内容没有出现变动就执行重启,出现变动就继续循环检测
作者: flashercs 时间: 2022-8-1 13:07
本帖最后由 flashercs 于 2022-8-1 15:46 编辑
回复 11# linlianboy - <#*,:&cls
- @echo off
- cd /d "%~dp0"
- powershell -C "Set-Location -LiteralPath ([Environment]::CurrentDirectory);. ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~f0\" -ReadCount 0 | Out-String)))"
-
- exit /b
- #>
- # 超时90分钟
- $span0 = New-TimeSpan -Minutes 90
- # ini file
- $inifile = ".\1.ini"
-
- function Get-DataHash {
- [CmdletBinding(DefaultParameterSetName = "Path")]
- [OutputType('Microsoft.Powershell.Utility.FileHash')]
- param(
- [Parameter(Mandatory = $true, ParameterSetName = "Path", ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Position = 0)]
- [System.String[]]
- $Path,
- [Parameter(Mandatory = $true, ParameterSetName = "LiteralPath", ValueFromPipelineByPropertyName = $true)]
- [ValidateNotNull()]
- [Alias("PSPath")]
- [System.String[]]
- $LiteralPath,
- [Parameter(Mandatory = $true, ParameterSetName = "Stream", ValueFromPipeline = $true, Position = 0)]
- [System.IO.Stream]
- $InputStream,
- [Parameter(Mandatory = $true, ParameterSetName = "Bytes", Position = 0)]
- [byte[]]
- $Buffer,
- [Parameter(Mandatory = $false, ParameterSetName = "Bytes")]
- [int]
- $Offset = -1,
- [Parameter(Mandatory = $false, ParameterSetName = "Bytes")]
- [int]
- $Count = -1,
- [Parameter(Mandatory = $false)]
- [ValidateSet("SHA1", "SHA256", "SHA384", "SHA512", "MD5", "RIPEMD160")]
- [System.String]
- $Algorithm = "SHA256",
- # Consider all input data as a single data.
- [switch]$AllTogether,
- # return Hash as byte[]
- [ValidateSet("Hex", "Base64", "Bytes")]
- [string]$HashMode = "Hex"
- )
- begin {
- # Construct the strongly-typed crypto object
- [System.Security.Cryptography.HashAlgorithm]$hashAlg = [System.Security.Cryptography.HashAlgorithm]::Create($Algorithm)
- $Algorithm = $Algorithm.ToUpperInvariant()
- if ($AllTogether) {
- $cryptoStream = New-Object System.Security.Cryptography.CryptoStream -ArgumentList ([System.IO.Stream]::Null, $hashAlg, 'Write')
- }
- function GetPso {
- param (
- [byte[]]$Hash,
- [string]$RelatedPath
- )
- ($pso = 0 | Select-Object -Property @{
- Name = "Algorithm"
- Expression = { $Algorithm }
- }, @{
- Name = "Hash"
- Expression = {
- switch ($HashMode) {
- "Hex" { [System.BitConverter]::ToString($Hash) -replace "-" }
- "Base64" { [convert]::ToBase64String($Hash) }
- "Bytes" { $Hash }
- }
- }
- }, @{
- Name = "Path"
- Expression = { $RelatedPath }
- }) | Add-Member -TypeName Microsoft.Powershell.Utility.FileHash
- $pso
- # New-Object psobject -Property ([ordered]@{
- # Algorithm = $Algorithm
- # Hash = [System.BitConverter]::ToString($Hash) -replace "-", ""
- # Path = $RelatedPath
- # })
- }
- }
- process {
- switch ($PSCmdlet.ParameterSetName) {
- "Stream" {
- if ($AllTogether) {
- $InputStream.CopyTo($cryptoStream)
- } else {
- GetPso -Hash ($hashAlg.ComputeHash($InputStream))
- }
- continue
- }
- "Bytes" {
- if ($AllTogether) {
- if ($Offset -lt 0 -or $Count -lt 0) {
- $cryptoStream.Write($Buffer, 0, $Buffer.Count)
- } else {
- $cryptoStream.Write($Buffer, $Offset, $Count)
- }
- } else {
- if ($Offset -lt 0 -or $Count -lt 0) {
- $hash = $hashAlg.ComputeHash($Buffer)
- } else {
- $hash = $hashAlg.ComputeHash($Buffer, $Offset, $Count)
- }
- GetPso -Hash $hash
- }
- continue
- }
- "Path" {
- $pathsToProcess = Convert-Path -Path $Path
- }
- "LiteralPath" {
- $pathsToProcess = Convert-Path -LiteralPath $LiteralPath
- }
- { $true } {
- foreach ($filePath in $pathsToProcess) {
- if (Test-Path -LiteralPath $filePath -PathType Container) {
- continue
- }
- try {
- # Read the file specified in $FilePath as a Byte array
- $stream = [System.IO.File]::OpenRead($filePath)
- if ($AllTogether) {
- $stream.CopyTo($cryptoStream)
- } else {
- GetPso -Hash ($hashAlg.ComputeHash($stream)) -RelatedPath $filePath
- }
- } catch {
- # $PSCmdlet.WriteError($_)
- } finally {
- if ($null -ne $stream) {
- $stream.Dispose()
- }
- }
- }
- break
- }
- }
- }
- end {
- if ($AllTogether) {
- $cryptoStream.Clear()
- $cryptoStream.Dispose()
- GetPso -Hash ($hashAlg.Hash)
- }
- $hashAlg.Clear()
- $hashAlg.Dispose()
- }
- }
- # main
- $pso = New-Object psobject
- $pso | Add-Member -MemberType NoteProperty -Name UpdateTime -Value (Get-Date)
- $pso | Add-Member -MemberType NoteProperty -Name CurrentTime -Value (Get-Date)
- $pso | Add-Member -MemberType ScriptProperty -Name TimeElapsed -Value { $this.CurrentTime - $this.UpdateTime }
- $pso | Add-Member -MemberType NoteProperty -Name FileHash -Value $null
- while ($true) {
- $pso.CurrentTime = Get-Date
- $timeOfDay = $pso.CurrentTime.TimeOfDay
- if ($timeOfDay -ge '00:00:00' -and $timeOfDay -lt '23:00:00') {
- $oHash = Get-DataHash -LiteralPath $inifile -Algorithm SHA256
- if ($pso.FileHash -ne $oHash.Hash) {
- $pso.FileHash = $oHash.Hash
- $pso.UpdateTime = $pso.CurrentTime
- }
- $pso
- if ($pso.TimeElapsed -gt $span0) {
- '超时1.5小时'
- $(
- & "C:\ldplayer\Ldconsole.exe" quitall
- choice /t 2 /d y /n
- taskkill /f /im my.exe
- taskkill /f/ im dnmultiplayer.exe
- taskkill /f /im LdVBoxHeadless.exe
- taskkill /f /im LdVBoxSVC.exe
- shutdown.exe /r /f /t 0
- )>$null 2>$null
- break
- } else {
- '未超时'
- }
- }
- Start-Sleep -Seconds 10
- }
复制代码
作者: linlianboy 时间: 2022-8-1 13:33
回复 12# flashercs
哇塞,代码量这么大的吗,大佬您头像的支付宝是收款码吗.我给你发个红包
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |