PowerShell/docs/replace-in-files.md

108 lines
3.3 KiB
Markdown
Raw Normal View History

2024-11-08 12:38:20 +01:00
The *replace-in-files.ps1* Script
===========================
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
2024-11-08 12:35:11 +01:00
/home/markus/Repos/PowerShell/scripts/replace-in-files.ps1 [[-pattern] <String>] [[-replacement] <String>] [[-filePattern] <String>] [<CommonParameters>]
2021-11-08 21:36:42 +01:00
-pattern <String>
2024-11-08 12:35:11 +01:00
Specifies the text pattern to search for (ask user by default)
2021-11-08 21:36:42 +01:00
Required? false
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters? false
-replacement <String>
2024-11-08 12:35:11 +01:00
Specifies the text replacement (ask user by default)
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>
2024-11-08 12:35:11 +01:00
Specifies the file search pattern (ask user by default)
2021-11-08 21:36:42 +01:00
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
2024-11-08 12:35:11 +01:00
Specifies the text pattern to search for (ask user by default)
2022-11-17 20:02:26 +01:00
.PARAMETER replacement
2024-11-08 12:35:11 +01:00
Specifies the text replacement (ask user by default)
2023-12-07 20:24:45 +01:00
.PARAMETER filePattern
2024-11-08 12:35:11 +01:00
Specifies the file search pattern (ask user by default)
2022-11-17 20:02:26 +01:00
.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
2024-11-08 12:35:11 +01:00
function ReplaceInFile([string]$path, [string]$pattern, [string]$replacement) {
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 {
2024-11-08 12:35:11 +01:00
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'" }
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
2024-11-08 12:35:11 +01:00
"✅ Replaced '$pattern' by '$replacement' in $($files.Count) files in $($elapsed)s."
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-11-08 12:40:31 +01:00
*(generated by convert-ps2md.ps1 as of 11/08/2024 12:40:21)*