Untitled
unknown
plain_text
2 years ago
1.2 kB
5
Indexable
param (
[string]$DirectoryName,
[string]$RegexPattern
)
# Check if the directory exists
if (-not (Test-Path $DirectoryName -PathType Container)) {
Write-Host "Directory '$DirectoryName' does not exist."
exit
}
# Get all text files in the specified directory
$textFiles = Get-ChildItem -Path $DirectoryName -Filter *.txt
# Array to store all matched lines
$allMatches = @()
# Loop through each text file
foreach ($file in $textFiles) {
Write-Host "Processing file $($file.FullName)"
# Read the content of the text file
$content = Get-Content -Path $file.FullName
# Match lines against the regex pattern
$matches = $content | Where-Object { $_ -match $RegexPattern }
# Remove the first 22 characters from each matched line
$trimmedMatches = $matches -replace '^.{0,22}', ''
# Add trimmed matched lines to the array
$allMatches += $trimmedMatches
}
# Output all matched lines to a new file
$outputFileName = "$DirectoryName\all_matches.txt"
$allMatches | Out-File -FilePath $outputFileName -Encoding utf8
Write-Host "All matched lines written to '$outputFileName'"Editor is loading...
Leave a Comment