Skip to content

Instantly share code, notes, and snippets.

@kneeprayer
Last active April 5, 2019 07:59
Show Gist options
  • Save kneeprayer/140f904934c0368314d025aea033412b to your computer and use it in GitHub Desktop.
Save kneeprayer/140f904934c0368314d025aea033412b to your computer and use it in GitHub Desktop.
Replace all contents in the files in a specific recurse path.
#===============================================================================
# Replace-Content: 特定パス配下の全てのファイル内の指定文字列を置換する
# Param:
# $filePath : ファイルパス
# $replaceText1 : 置換対象文字列
# $replaceText2 : 置換後の文字列
#
# 使用例
# #sample.txtのファイル内の"AAA"という文字列を"BBB"に置換する
# Get-Content "C:\Work\*" "AAA" "BBB"
#
#===============================================================================
Function Global:Replace-Content {
Param([String]$filePath, [String]$replaceText1, [String]$replaceText2)
if ( $(Test-Path $filePath) -ne $True) {
Write-Error "Wrong Path"
return
}
Get-ChildItem $filePath -Recurse |`
Where-Object {! $_.PSIsContainer } |`
ForEach-Object {`
echo (($_ | Get-Content) -replace $replaceText1,$replaceText2) > $_.FullName `
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment