回复 31# w31575801
log中还有些行开头不是时间格式,里面还有叹号!- <#*,:&cls
- @echo off
- pushd "%~dp0"
- powershell -NoProfile -ExecutionPolicy RemoteSigned -Command ". ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~0\" -ReadCount 0 | Out-String ))) "
- popd
- pause
- exit /b
- #>
- # 起始时间
- $dtmBegin = "02:31:37.501"
- # 终止时间
- $dtmEnd = "03:42:00.140"
- $logglob = "*.log"
- $dtmLength = $dtmBegin.Length
- $dtmFormat = "[0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9][0-9]"
- $buffer = 65536
- # utf8-nobom
- $encoding = New-Object System.Text.UTF8Encoding -ArgumentList @($false)
- foreach ($logfile in (Convert-Path -Path $logglob | Where-Object { Test-Path -LiteralPath $_ -PathType Leaf })) {
- Write-Host $logfile -ForegroundColor Green
- try {
- $stream1 = New-Object System.IO.FileStream -ArgumentList @($logfile, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::ReadWrite, $buffer)
- $stream2 = New-Object System.IO.FileStream -ArgumentList @($logfile, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Write, [System.IO.FileShare]::Read, $buffer)
- $sr = New-Object System.IO.StreamReader -ArgumentList @($stream1, $encoding, $true, $buffer)
- $sw = New-Object System.IO.StreamWriter -ArgumentList @($stream2, $encoding, $buffer)
- $flag = $false
- while (!$sr.EndOfStream) {
- $strLine = $sr.ReadLine()
- $dtmStr = $strLine.Substring(0, [Math]::Min($dtmLength, $strLine.Length))
- if ($dtmStr -notlike $dtmFormat) {
- $sw.WriteLine($strLine)
- continue
- }
- if (!$flag) {
- if ($dtmStr -ge $dtmBegin) {
- if ($dtmStr -lt $dtmEnd) {
- $sw.WriteLine($strLine)
- $flag = $true
- } else {
- break
- }
- }
- } else {
- if ($dtmStr -lt $dtmEnd) {
- $sw.WriteLine($strLine)
- } else {
- $flag = $false
- break
- }
- }
- }
- $sw.Flush()
- $stream2.SetLength($stream2.Position)
- } catch {
- $_ | Write-Host -ForegroundColor Red
- } finally {
- if ($sr) {
- $sr.Dispose()
- }
- if ($sw) {
- $sw.Dispose()
- }
- if ($stream1) {
- $stream1.Dispose()
- }
- if ($stream2) {
- $stream2.Dispose()
- }
- }
- }
复制代码
|