Skip to content

Instantly share code, notes, and snippets.

@tomodachi94
Created August 29, 2024 04:17
Show Gist options
  • Save tomodachi94/39465c2895f366c10fde2fdfc4542db2 to your computer and use it in GitHub Desktop.
Save tomodachi94/39465c2895f366c10fde2fdfc4542db2 to your computer and use it in GitHub Desktop.
batch-replace.ps1
#!/usr/bin/pwsh
# see ./README.md for more information
function Replace-Safely {
[CmdletBinding()]
param(
[Parameter()]
[string] $old
[string] $new
[string] $path
)
$path = Get-ChildItem -Path Env:\Script-TargetReplaceFile
cd $path
git add $path
git commit -m "Automatic commit before processing" # if no changes, the commit attempt will be ignored
((Get-Content -path $path -Raw) -replace $old,$new) | Set-Content -Path $path # Replaces all occurances of "foo" with "bar"
git add $path
git commit -m 'Replace ' $old ' with ' $new
}
Replace-Safely -old foo -new bar -path C:\AbsolutePath\To\Notes.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment