Replace String in file contents and file names using PowerShell


/ Published in: Windows PowerShell
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function Replace-String($find, $replace, $path)
  2. {
  3. echo "Replacing string `"$find`" with string `"$replace`" in file contents and file names of path: $path"
  4. ls $path | select-string $find -list |% { echo "Processing contents of $($_.Path)"; (get-content $_.Path) |% { $_ -replace $find, $replace } | set-content $_.Path -Force }
  5. ls $path\*$find* |% { echo "Renaming $($_.FullName) to $($_.FullName.Replace($find, $replace))";mv $_.FullName $_.FullName.Replace($find, $replace) }
  6. }
  7.  
  8. # Example
  9. Replace-String "Temp1" "Temp2" "D:\Temp"

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.