Aqui está a solução do PowerShell usando Streams. Leva ~ 60 segundos para processar o arquivo de 1GB no meu laptop ( i3 2.5GHz, 4GB de RAM, 5400RPM HDD) . Ainda é três vezes mais lento do que LogParser , embora:
LOGPARSER "Select Text from C:\Path\To\log.file where Text like '01/22/15%'" -i:TEXTLINE -q:Off
$LogFolder = 'c:\Path\To\Log\Folder'
$Filter = '*.log'
$Postfix = '.tmp'
$cutDatePtrn = '^' + (Get-Date).AddDays(-30).ToString('MM/dd/yy')
# Iterate over each log file in the $LogFolder
Get-ChildItem -LiteralPath $LogFolder -Filter $Filter |
ForEach-Object {
Write-Host "Current file: $($_.FullName)"
$InFile = New-Object -TypeName System.IO.StreamReader -ArgumentList $_.FullName
Write-Host 'Processing file...'
$WriteFile = $false
while(($line = $InFile.ReadLine()) -ne $null)
{
if((-not $WriteFile) -and ($line -notmatch $cutDatePtrn))
{
continue
}
elseif((-not $WriteFile) -and ($line -match $cutDatePtrn))
{
Write-Host 'Found match:'
Write-Host $line
$WriteFile = $true
$TmpFile = $_.FullName + $Postfix
Write-Host "Creating new temporary file: $TmpFile"
$OutFile = New-Object -TypeName System.IO.StreamWriter -ArgumentList $TmpFile, $false
}
$OutFile.WriteLine($line)
}
Write-Host 'Done processing, cleaning up...'
if($OutFile)
{
$OutFile.Flush()
$OutFile.Close()
$OutFile.Dispose()
}
$InFile.Close()
$InFile.Dispose()
if(Test-Path $TmpFile -PathType Leaf)
{
Write-Host "Deleting original file: $($_.FullName)"
Remove-Item -Path $_.FullName -Force
Write-Host "Renaming temporary file: $TmpFile -> $($_.FullName)"
Rename-Item -Path $TmpFile -NewName $_.FullName -Force
}
Write-Host "Finished processing file: $($_.FullName)"
}