Monday, August 30, 2010

Awww my first post

So I wanted a function that I could use to find/replace text in multiple files in a directory with a recursive option, and this is what I came up with. I had help from another blog post, but cant remember the author)

function Update-Content($find, $replace, $path, [switch]$Recurse)
{
if($Recurse){
get-childitem $path -Recurse | select-string $find -list |% { (get-content $_.Path) |% { $_ -replace $find, $replace } | set-content $_.Path }
}
else{
get-childitem $path | select-string $find -list |% { (get-content $_.Path) |% { $_ -replace $find, $replace } | set-content $_.Path }
}
}

No comments:

Post a Comment