Уже было несколько таких вопросов, но ни один из них не был достаточно конкретным для моих целей.
Мне нужно выполнить поиск в 10 000 файлов журнала для определенной строки, а затем вывести каждую строку, в которой находится эта строка, а также создать копию этого файла журнала.
У меня это почти работает в пакетном файле. Я думаю, что уперся в стену, и мне нужно начать использовать powershell, который я раньше не использовал.
:Обновить
Благодаря Trondh я смог использовать его сценарий как идеальную основу и добавить необходимые мне функции. Надеюсь, это поможет кому-то еще :)
#Folder to search
$folder = read-host "Please specify the location of the search "
#Search for:
$SearchTerm = read-host "type in the word you want to find Eg. Error or JobID "
#Files to include in search
$FileFilter = read-host "Enter Date Part of file filter Eg. 20140123 or 201401 "
#File to store log file copies in
$destinationfolder = "Backup-$SearchTerm"
#File to store results in
$newfile = "Search-Log-$SearchTerm.txt"
#Get the files according to filter. Recurse, but exclude directories
$files = Get-ChildItem -Path $folder -Include @("*$filefilter*.*") -recurse | where {$_.PSIsContainer -eq $false}
foreach ($file in $files)
{
$result = $file | Select-String $SearchTerm
$result | add-content $newfile
New-Item -ItemType Directory -Force -Path $destinationfolder
#If we get a hit, copy the file
if ($result)
{
Write-host "Found match in file $($file.Name) ($($file.Directory))"
#Add result to file
$file | Copy-Item -Destination $destinationfolder
#Also output it
$result
}
}
Write-Host "Search Completed!"
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")