I have a PowerShell script:
(Select-String -path c:\*.txt -Pattern 'searchstring').Line | set-content d:\output.txt
It works fine but the output file is too big. What I would like is for it to output multiple files so output.txt
is the same number of files as is being read in with the wild card.
So:
c:\1.txt
c:\2.txt
c:\3.txt
Would output the matched lines to:
d:\1.txt
d:\2.txt
d:\3.txt
CodePudding user response:
As Santiago Squarzon suggests, use Get-ChildItem
to retrieve the target files, then call Select-String
on them one by one:
Get-ChildItem c:\*.txt |
ForEach-Object {
if ($lines = ($_ | Select-String -Pattern 'SearchPattern').Line) {
Set-Content -LiteralPath (Join-Path d:\ $_.Name) -Value $lines
}
}