mirror of
https://github.com/fleschutz/PowerShell.git
synced 2024-11-08 09:04:18 +01:00
Add replace-in-files.ps1
This commit is contained in:
parent
9a1f0113e0
commit
3fa508fa9c
@ -189,6 +189,7 @@ reboot-fritzbox.ps1, Reboots the FRITZ!box device
|
||||
remove-empty-dirs.ps1, Removes empty subfolders within the given directory tree
|
||||
remove-print-jobs.ps1, Removes all jobs from all printers
|
||||
remove-tag.ps1, Removes a tag in a Git repository
|
||||
replace-in-files.ps1, Search and replace a pattern in the given files by the replacement
|
||||
restart-network-adapters.ps1, Restarts all local network adapters
|
||||
search-filename.ps1, Searches the directory tree for filenames by given pattern
|
||||
search-files.ps1, Searches the given pattern in the given files
|
||||
|
Can't render this file because it has a wrong number of fields in line 87.
|
@ -172,6 +172,7 @@ Mega Collection of PowerShell Scripts
|
||||
| [new-zipfile.ps1](Scripts/new-zipfile.ps1) | Creates a new .zip file from a directory | [Help](Docs/new-zipfile.ps1.md) |
|
||||
| [publish-to-ipfs.ps1](Scripts/publish-to-ipfs.ps1) | Publishes the given files or directory to IPFS | [Help](Docs/publish-to-ipfs.ps1.md) |
|
||||
| [remove-empty-dirs.ps1](Scripts/remove-empty-dirs.ps1) | Removes empty subfolders within the given directory tree | [Help](Docs/remove-empty-dirs.ps1.md) |
|
||||
| [replace-in-files.ps1](Scripts/replace-in-files.ps1) | Search and replace a pattern in the given files by the replacement | [Help](Docs/replace-in-files.ps1.md) |
|
||||
| [search-filename.ps1](Scripts/search-filename.ps1) | Searches the directory tree for filenames by given pattern | [Help](Docs/search-filename.ps1.md) |
|
||||
| [search-files.ps1](Scripts/search-files.ps1) | Searches the given pattern in the given files | [Help](Docs/search-files.ps1.md) |
|
||||
| [upload-file.ps1](Scripts/upload-file.ps1) | Uploads the local file to the given FTP server | [Help](Docs/upload-file.ps1.md) |
|
||||
|
35
Scripts/replace-in-files.ps1
Normal file
35
Scripts/replace-in-files.ps1
Normal file
@ -0,0 +1,35 @@
|
||||
<#
|
||||
.SYNOPSIS
|
||||
replace-in-files.ps1 [<pattern>] [<replacement>] [<files>]
|
||||
.DESCRIPTION
|
||||
Search and replace a pattern in the given files by the replacement
|
||||
.EXAMPLE
|
||||
PS> ./replace-in-files IO "Input Output" C:\Temp\*.txt
|
||||
.NOTES
|
||||
Author: Markus Fleschutz · License: CC0
|
||||
.LINK
|
||||
https://github.com/fleschutz/PowerShell
|
||||
#>
|
||||
|
||||
param([string]$pattern = "", [string]$replacement = "", [string]$files = "")
|
||||
|
||||
function ReplaceInFile { param([string]$FilePath, [string]$Pattern, [string]$Replacement)
|
||||
|
||||
[System.IO.File]::WriteAllText($FilePath,
|
||||
([System.IO.File]::ReadAllText($FilePath) -replace $Pattern, $Replacement)
|
||||
)
|
||||
}
|
||||
|
||||
try {
|
||||
if ($pattern -eq "" ) { $pattern = read-host "Enter search pattern" }
|
||||
if ($replacement -eq "" ) { $replacement = read-host "Enter replacement" }
|
||||
if ($files -eq "" ) { $files = read-host "Enter files" }
|
||||
|
||||
foreach($file in $files) {
|
||||
ReplaceInFile $file $pattern $replacement
|
||||
}
|
||||
exit 0
|
||||
} catch {
|
||||
"⚠️ Error: $($Error[0]) ($($MyInvocation.MyCommand.Name):$($_.InvocationInfo.ScriptLineNumber))"
|
||||
exit 1
|
||||
}
|
Loading…
Reference in New Issue
Block a user