Search and Replace regexp in subdirectories


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

replace foo -> bar in all .txt files down the tree

Dunno why, the syntax highlighter adds \3's to the code. Try plain text.


Copy this code and paste it in your HTML
  1. # Select files
  2. $files = gci . -recurse | ? { ($_ -match "txt$") -and ((gc $_.VersionInfo.FileName) -match "the_sink") } | % { $_.VersionInfo.FileName }
  3.  
  4. # Back'em up, just in case:
  5. $files | % { cp $_ "$($_).bak" }
  6.  
  7. # Replace
  8. $files | %{ (gc $_) -replace "foo","bar" | sc -Path $_ }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.