-
-
Notifications
You must be signed in to change notification settings - Fork 424
/
replace-in-files.ps1
executable file
·44 lines (40 loc) · 1.61 KB
/
replace-in-files.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<#
.SYNOPSIS
Search and replace a pattern in the given files by the replacement
.DESCRIPTION
This PowerShell script searches and replaces a pattern in the given files by the replacement.
.PARAMETER pattern
Specifies the text pattern to search for (ask user by default)
.PARAMETER replacement
Specifies the text replacement (ask user by default)
.PARAMETER filePattern
Specifies the file search pattern (ask user by default)
.EXAMPLE
PS> ./replace-in-files NSA "No Such Agency" C:\Temp\*.txt
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$pattern = "", [string]$replacement = "", [string]$filePattern = "")
function ReplaceInFile([string]$path, [string]$pattern, [string]$replacement) {
[System.IO.File]::WriteAllText($path,
([System.IO.File]::ReadAllText($path) -replace $pattern, $replacement)
)
}
try {
if ($pattern -eq "" ) { $pattern = Read-Host "Enter the text to search for, e.g. 'Joe' " }
if ($replacement -eq "" ) { $replacement = Read-Host "Enter the text to replace with, e.g. 'J' " }
if ($filePattern -eq "" ) { $filePattern = Read-Host "Enter the file search pattern, e.g. '*.c'" }
$stopWatch = [system.diagnostics.stopwatch]::startNew()
$files = (Get-ChildItem -path "$filePattern" -attributes !Directory)
foreach($file in $files) {
ReplaceInFile $file $pattern $replacement
}
[int]$elapsed = $stopWatch.Elapsed.TotalSeconds
"✅ Replaced '$pattern' by '$replacement' in $($files.Count) files in $($elapsed)s."
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}