From d000abf675a76b9cd2e4e13ebf698243734a905b Mon Sep 17 00:00:00 2001 From: KnifMelti Date: Wed, 18 Jan 2023 00:53:52 +0100 Subject: [PATCH] Mods Find/Replace text in file --- Winget-AutoUpdate/mods/_AppID-template.ps1 | 12 ++++++++++++ Winget-AutoUpdate/mods/_Mods-Functions.ps1 | 7 +++++++ 2 files changed, 19 insertions(+) diff --git a/Winget-AutoUpdate/mods/_AppID-template.ps1 b/Winget-AutoUpdate/mods/_AppID-template.ps1 index 7dda381..bec1128 100644 --- a/Winget-AutoUpdate/mods/_AppID-template.ps1 +++ b/Winget-AutoUpdate/mods/_AppID-template.ps1 @@ -44,6 +44,15 @@ $DelFile = @("") $CopyFile = "" $CopyTo = "" +#Find/Replace text in file +#Example: +#$ModsFile = "C:\dummy.txt" +#$FindText = "brown fox" +#$ReplaceText = "white fox" +$ModsFile = "" +$FindText = "" +$ReplaceText = "" + #Grant "Modify" for directory/file to "Authenticated Users" - multiple: "dir1","dir2" $GrantPath = @("") @@ -78,6 +87,9 @@ if ($DelFile) { if ($CopyFile -and $CopyTo) { Copy-ModsFile $CopyFile $CopyTo } +if ($ModsFile -and $FindText -and $ReplaceText) { + Edit-ModsFile $ModsFile $FindText $ReplaceText +} if ($GrantPath) { Grant-ModsPath $GrantPath } diff --git a/Winget-AutoUpdate/mods/_Mods-Functions.ps1 b/Winget-AutoUpdate/mods/_Mods-Functions.ps1 index c76eb95..51c8006 100644 --- a/Winget-AutoUpdate/mods/_Mods-Functions.ps1 +++ b/Winget-AutoUpdate/mods/_Mods-Functions.ps1 @@ -192,6 +192,13 @@ function Copy-ModsFile ($CopyFile, $CopyTo) { Return } +function Edit-ModsFile ($ModsFile, $FindText, $ReplaceText) { + if (Test-Path "$ModsFile") { + ((Get-Content -path $ModsFile -Raw) -replace "$FindText","$ReplaceText") | Set-Content -Path $ModsFile -Force -ErrorAction SilentlyContinue | Out-Null + } + Return +} + function Grant-ModsPath ($GrantPath) { foreach ($path in $GrantPath) {