Return to Snippet

Revision: 36140
at December 3, 2010 19:01 by jarnaldich


Updated Code
# Select files
$files = gci . -recurse | ? { ($_ -match "txt$") -and ((gc $_.VersionInfo.FileName) -match "the_sink") } | % { $_.VersionInfo.FileName }

# Back'em up, just in case:
$files | % { cp $_ "$($_).bak" }

# Replace
$files | %{ (gc $_) -replace "foo","bar" | sc -Path $_ }

Revision: 36139
at December 3, 2010 19:00 by jarnaldich


Updated Code
# Select files
$files = gci . -recurse | ? { ($_ -match "txt$") -and ((gc $_\.VersionInfo.FileName) -match "the_sink") } | % { $_.VersionInfo.FileName }

# Back'em up, just in case:
$files | % { cp $_ "$($_).bak" }

# Replace
$files | %{ (gc $_) -replace "foo","bar" | sc -Path $_ }

Revision: 36138
at November 18, 2010 19:58 by jarnaldich


Updated Code
# Select files
$files = gci . -recurse | ? { ($_ -match "txt$") -and ((gc $_.VersionInfo.FileName) -match "the_sink") } | % { $_.VersionInfo.FileName }

# Back'em up, just in case:
$files | % { cp $_ "$($_).bak" }

# Replace
$files | %{ (gc $_) -replace "foo","bar" | sc -Path $_ }

Revision: 36137
at November 18, 2010 19:31 by jarnaldich


Initial Code
# Select files
$files = Select-String -Path .\**\*.txt -Pattern "foo" | % { $_.Path } | Get-Unique

# Back'em up, just in case:
$files | % { Copy-Item $_ "$($_).bak" }

# Replace
$files | %{ (Get-Content $_) -replace "foo","bar" | Set-Content -Path $_ }

Initial URL


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

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

Initial Title
Search and Replace regexp in subdirectories

Initial Tags
regex, search, replace, regexp

Initial Language
Windows PowerShell