- <#*,:&cls
- @echo off
- cd /d "%~dp0"
- powershell -C "Set-Location -LiteralPath ([Environment]::CurrentDirectory);. ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~f0\" -ReadCount 0 | Out-String)))"
- pause
- exit /b
- #>
- # 功能:按时间搜索指定目录下txt文档中的字符串,按ctrl+c退出
- $rootdir = "\\192.168.11.123\pool\test123\p01-1"
-
- $outfile = "搜索结果.log"
-
- $arrPso = foreach ($arrLines in Get-Content -ReadCount 0 -Path "$rootdir\*.txt" -Filter *.txt -ErrorAction SilentlyContinue) {
- foreach ($line in $arrLines) {
- $sDate, $sTime, $sText = $line -split '\s+', 3
- ($pso = New-Object psobject -Property @{
- Date = [datetime]"$sDate $sTime"
- Text = $sText
- })
- }
- }
- $Host.UI.RawUI.WindowTitle = "按 Ctrl+C 退出"
- # exit
- while ($true) {
- do {
- $startTime = $null
- $startTime = (Read-Host -Prompt 请输入起始时间) -as [datetime]
- } while ($null -eq $startTime)
- do {
- $endTime = $null
- $endTime = (Read-Host -Prompt 请输入结束时间) -as [datetime]
- } while ($null -eq $endTime)
- $strSearch = Read-Host -Prompt 请输入查找字符串
- $alResult = $null
- $arrPso | Where-Object { $_.Date -ge $startTime -and $_.Date -lt $endTime -and $_.Text.Contains($strSearch) } -OutVariable alResult | Format-Table -AutoSize
- "$startTime $endTime $strSearch $($alResult.Count)" | Add-Content -LiteralPath $outfile
- }
复制代码
|