Created
December 29, 2011 07:56
-
-
Save jongalloway/1532778 to your computer and use it in GitHub Desktop.
Recursive replace in files (PowerShell)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$find = 'jquery-1\.4\.4' | |
$replace = 'jquery-1\.5\.1' | |
$match = '*.cshtml' , '*.vbhtml' | |
$preview = $true | |
foreach ($sc in dir -recurse -include $match | where { test-path $_.fullname -pathtype leaf} ) { | |
select-string -path $sc -pattern $find | |
if (!$preview) { | |
(get-content $sc) | foreach-object { $_ -replace $find, $replace } | set-content $sc | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
dir -r -i $match | sls $find -list | select -exp Path | %{
echo 'more stuff here, maybe tf edit ...';
(gc $) | # in parens so file won't be in use during Set-Content
%{ $ -replace $find,$replace } |
sc $_ -WhatIf:$preview # to keep variable in control, if that is being passed as param
}
If you need the Select-String you can place it inline (in case you need to check-out the file from archaic SCM, say TFS). Personally I dislike using GCI, I really prefer DIR or LS. I'm really surprised SLS doesn't have a built-in recurse param (grep), until then I'll keep hiding it behind 'DIR -R'.