Advertisement
joedigital

replace text (multiple files, multiple replacements)

Apr 1st, 2025
1,005
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 2.58 KB | Source Code | 0 0
  1. <# sample command line
  2. .\sanitizeLinkfixerScans.ps1 -SourcePath "G:\Shared drives\" -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=1
  28.   foreach ($csvFile in $sanitizeFiles) {
  29.     $fullFileName = $SourcePath + $csvFile
  30.       $textToReplace = [System.IO.File]::ReadAllText("$fullFileName") #read the file once
  31.   <#
  32.   #The .Replace() method, when used directly on a string object, is case-sensitive.
  33.  
  34.   todo: delete the first 4 lines
  35.     regex: starts with "Computer Name:..." ends with "delete these first four rows."
  36.   #>
  37.       $textToReplace = $textToReplace.Replace('LinkTek Support via phone (727-442-1822) or email ([email protected])', 'support')
  38.       $textToReplace = $textToReplace.Replace('LinkTek Support via email at [email protected] or call 727-442-1822', 'support')
  39.       $textToReplace = $textToReplace.Replace('Support via email at [email protected] or call 727-442-1822', 'support')
  40.       $textToReplace = $textToReplace.Replace('[email protected] or call 727-442-1822', 'support')
  41.       $textToReplace = $textToReplace.Replace('[email protected] or calling 727-442-1822', 'support')
  42.       $textToReplace = $textToReplace.Replace('[email protected]', 'support')
  43.       $textToReplace = $textToReplace.Replace('LinkTek Support', 'support')
  44.       $textToReplace = $textToReplace.Replace('LinkTek support', 'support')
  45.       $textToReplace = $textToReplace.Replace('help at 727-442-1822', 'support')
  46.       $textToReplace = $textToReplace.Replace('LinkFixer Advanced', 'analysis')
  47.       $oFile = $SourcePath + $OutputFilePrefix + $csvFile
  48.     [System.IO.File]::WriteAllText($oFile, $textToReplace)
  49.     Write-Host $csvFile "($myCounter of $myCountdown)"
  50.     $myCounter++
  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