2024-01-25 13:37:12 +01:00
|
|
|
Script: *replace-in-files.ps1*
|
|
|
|
========================
|
2021-11-08 21:36:42 +01:00
|
|
|
|
2022-02-10 09:01:07 +01:00
|
|
|
This PowerShell script searches and replaces a pattern in the given files by the replacement.
|
2021-11-08 21:36:42 +01:00
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Parameters
|
|
|
|
----------
|
2021-11-08 21:36:42 +01:00
|
|
|
```powershell
|
2023-12-07 20:24:45 +01:00
|
|
|
PS> ./replace-in-files.ps1 [[-pattern] <String>] [[-replacement] <String>] [[-filePattern] <String>] [<CommonParameters>]
|
2021-11-08 21:36:42 +01:00
|
|
|
|
|
|
|
-pattern <String>
|
2023-12-07 20:24:45 +01:00
|
|
|
Specifies the text pattern to look for
|
2021-11-08 21:36:42 +01:00
|
|
|
|
|
|
|
Required? false
|
|
|
|
Position? 1
|
|
|
|
Default value
|
|
|
|
Accept pipeline input? false
|
|
|
|
Accept wildcard characters? false
|
|
|
|
|
|
|
|
-replacement <String>
|
2023-12-07 20:24:45 +01:00
|
|
|
Specifies the text replacement
|
2021-11-08 21:36:42 +01:00
|
|
|
|
|
|
|
Required? false
|
|
|
|
Position? 2
|
|
|
|
Default value
|
|
|
|
Accept pipeline input? false
|
|
|
|
Accept wildcard characters? false
|
|
|
|
|
2023-12-07 20:24:45 +01:00
|
|
|
-filePattern <String>
|
2021-11-08 21:36:42 +01:00
|
|
|
Specifies the file to scan
|
|
|
|
|
|
|
|
Required? false
|
|
|
|
Position? 3
|
|
|
|
Default value
|
|
|
|
Accept pipeline input? false
|
|
|
|
Accept wildcard characters? false
|
|
|
|
|
|
|
|
[<CommonParameters>]
|
|
|
|
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
|
|
|
|
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
|
|
|
|
```
|
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Example
|
|
|
|
-------
|
2021-11-08 21:36:42 +01:00
|
|
|
```powershell
|
|
|
|
PS> ./replace-in-files NSA "No Such Agency" C:\Temp\*.txt
|
|
|
|
|
|
|
|
```
|
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Notes
|
|
|
|
-----
|
2022-11-17 19:46:02 +01:00
|
|
|
Author: Markus Fleschutz | License: CC0
|
2021-11-08 21:36:42 +01:00
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Related Links
|
|
|
|
-------------
|
2021-11-08 21:36:42 +01:00
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Script Content
|
|
|
|
--------------
|
2022-11-17 20:05:34 +01:00
|
|
|
```powershell
|
2022-11-17 20:02:26 +01:00
|
|
|
<#
|
|
|
|
.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
|
2023-12-07 20:24:45 +01:00
|
|
|
Specifies the text pattern to look for
|
2022-11-17 20:02:26 +01:00
|
|
|
.PARAMETER replacement
|
2023-12-07 20:24:45 +01:00
|
|
|
Specifies the text replacement
|
|
|
|
.PARAMETER filePattern
|
2022-11-17 20:02:26 +01:00
|
|
|
Specifies the file to scan
|
|
|
|
.EXAMPLE
|
|
|
|
PS> ./replace-in-files NSA "No Such Agency" C:\Temp\*.txt
|
|
|
|
.LINK
|
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
.NOTES
|
|
|
|
Author: Markus Fleschutz | License: CC0
|
|
|
|
#>
|
|
|
|
|
2023-12-07 20:24:45 +01:00
|
|
|
param([string]$pattern = "", [string]$replacement = "", [string]$filePattern = "")
|
2022-11-17 20:02:26 +01:00
|
|
|
|
2023-12-07 20:24:45 +01:00
|
|
|
function ReplaceInFile { param([string]$path, [string]$pattern, [string]$replacement)
|
2022-11-17 20:02:26 +01:00
|
|
|
|
2023-12-07 20:24:45 +01:00
|
|
|
[System.IO.File]::WriteAllText($path,
|
|
|
|
([System.IO.File]::ReadAllText($path) -replace $pattern, $replacement)
|
2022-11-17 20:02:26 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-12-07 20:24:45 +01:00
|
|
|
if ($pattern -eq "" ) { $pattern = Read-Host "Enter the text pattern to look for" }
|
|
|
|
if ($replacement -eq "" ) { $replacement = Read-Host "Enter the text replacement" }
|
|
|
|
if ($filePattern -eq "" ) { $filePattern = Read-Host "Enter the file pattern" }
|
2022-11-17 20:02:26 +01:00
|
|
|
|
2023-12-07 20:24:45 +01:00
|
|
|
$stopWatch = [system.diagnostics.stopwatch]::startNew()
|
|
|
|
$files = (Get-ChildItem -path "$filePattern" -attributes !Directory)
|
|
|
|
foreach($file in $files) {
|
2022-11-17 20:02:26 +01:00
|
|
|
ReplaceInFile $file $pattern $replacement
|
|
|
|
}
|
2023-12-07 20:24:45 +01:00
|
|
|
[int]$elapsed = $stopWatch.Elapsed.TotalSeconds
|
|
|
|
"✔️ Replaced '$pattern' by '$replacement' in $($files.Count) files in $elapsed sec"
|
2022-11-17 20:02:26 +01:00
|
|
|
exit 0 # success
|
|
|
|
} catch {
|
|
|
|
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
|
|
|
exit 1
|
|
|
|
}
|
2022-11-17 20:05:34 +01:00
|
|
|
```
|
2022-11-17 20:02:26 +01:00
|
|
|
|
2024-08-15 09:51:46 +02:00
|
|
|
*(generated by convert-ps2md.ps1 using the comment-based help of replace-in-files.ps1 as of 08/15/2024 09:50:53)*
|