Posted By

dreamlusion on 09/04/08


Tagged


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

dantreacy
LeeRJohnson
Tyster
michanne


Replace String in file contents and file names using PowerShell


 / Published in: Windows PowerShell
 

  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 $_\3.Path) |% { $_\3 -replace $find, $replace } | set-content $_\3.Path -Force }
  5. ls $path\*$find* |% { echo "Renaming $($_.FullName) to $($_.FullName.Replace($find, $replace))";mv $_\3.FullName $_\3.FullName.Replace($find, $replace) }
  6. }
  7.  
  8. # Example
  9. Replace-String "Temp1" "Temp2" "D:\Temp"

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: Vordreller on April 19, 2009

a bit more explanation would be nice.

Posted By: omendez on August 7, 2009

thanks for the function. I've saved some time with it.

You need to login to post a comment.