Advertisement
joedigital

replace text (multiple files, multiple replacements)

Apr 1st, 2025 (edited)
1,395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 2.80 KB | Source Code | 0 0
  1. <# sample command line
  2. .\sanitizeLinkfixerScans.ps1 -SourcePath "G:\Shared drives\LinkFixer Scans\" -fileFilter "Scan Detail*" -OFilePrefix "sanitized-"
  3.  
  4. #assign params from command line
  5. #>
  6. param(
  7.   [Alias("UNCPath")]
  8.   [Parameter(Mandatory)]
  9.     [string]$SourcePath, #trailing backslash is required
  10.   [Parameter(Mandatory)]
  11.     [string]$fileFilter,
  12.   [Alias("OFilePrefix")]
  13.   [Parameter(Mandatory)]
  14.     [string]$OutputFilePrefix
  15. )
  16.  
  17. $sanitizeFiles = (Get-ChildItem -Path $SourcePath -Filter $fileFilter).Name #.Name does not include the SourcePath
  18. $mycountdown = $sanitizeFiles.count
  19.  
  20. if ($mycountdown -eq 0) {
  21.   Write-Host "exiting, no files matching '$fileFilter' in '$SourcePath'" -ForegroundColor Green -backgroundColor DarkGray
  22.   exit
  23.   }
  24. else {
  25.   $begTime = Get-Date
  26.   #$begTime
  27.   $myCounter=0
  28.   foreach ($csvFile in $sanitizeFiles) {
  29.     $myCounter++
  30.     $fullFileName = $SourcePath + $csvFile
  31.       $textToReplace = [System.IO.File]::ReadAllText("$fullFileName") #read the file once
  32.    
  33.       <#The .Replace() method, when used directly on a string object, is case-sensitive.
  34.       #-Replace regex: starts with "Computer Name:..." ends with "Log Time" so a blank line is not left at the top.
  35.         This also ensures 1 replacement which is at the beginning of the file where "Log Time" appears (row 5).
  36.       #>
  37.       $textToReplace = $textToReplace -replace '(?s)\"Computer Name:.*?\"Log Time','"Log Time'  
  38.       $textToReplace = $textToReplace.Replace('LinkTek Support via phone (727-442-1822) or email ([email protected])', 'support')
  39.       $textToReplace = $textToReplace.Replace('LinkTek Support via email at [email protected] or call 727-442-1822', 'support')
  40.       $textToReplace = $textToReplace.Replace('Support via email at [email protected] or call 727-442-1822', 'support')
  41.       $textToReplace = $textToReplace.Replace('[email protected] or call 727-442-1822', 'support')
  42.       $textToReplace = $textToReplace.Replace('[email protected] or calling 727-442-1822', 'support')
  43.       $textToReplace = $textToReplace.Replace('[email protected]', 'support')
  44.       $textToReplace = $textToReplace.Replace('LinkTek Support', 'support')
  45.       $textToReplace = $textToReplace.Replace('LinkTek support', 'support')
  46.       $textToReplace = $textToReplace.Replace('help at 727-442-1822', 'support')
  47.       $textToReplace = $textToReplace.Replace('LinkFixer Advanced', 'analysis')
  48.       $oFile = $SourcePath + $OutputFilePrefix + $csvFile
  49.     [System.IO.File]::WriteAllText($oFile, $textToReplace)
  50.     Write-Host $csvFile "($myCounter of $myCountdown)"
  51.   }
  52.   $endTime = Get-Date
  53.   #$endTime
  54.   New-TimeSpan -Start $begTime -End $endTime | Select-Object -Property TotalSeconds
  55.  
  56.   Write-Host "end of script: $myCounter of $myCountdown sanitized"  -ForegroundColor Green  -backgroundColor DarkGray
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement