Skip to content

Latest commit

 

History

History
107 lines (88 loc) · 3.27 KB

replace-in-files.md

File metadata and controls

107 lines (88 loc) · 3.27 KB

The replace-in-files.ps1 Script

This PowerShell script searches and replaces a pattern in the given files by the replacement.

Parameters

/home/markus/Repos/PowerShell/scripts/replace-in-files.ps1 [[-pattern] <String>] [[-replacement] <String>] [[-filePattern] <String>] [<CommonParameters>]

-pattern <String>
    Specifies the text pattern to search for (ask user by default)
    
    Required?                    false
    Position?                    1
    Default value                
    Accept pipeline input?       false
    Accept wildcard characters?  false

-replacement <String>
    Specifies the text replacement (ask user by default)
    
    Required?                    false
    Position?                    2
    Default value                
    Accept pipeline input?       false
    Accept wildcard characters?  false

-filePattern <String>
    Specifies the file search pattern (ask user by default)
    
    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.

Example

PS> ./replace-in-files NSA "No Such Agency" C:\Temp\*.txt

Notes

Author: Markus Fleschutz | License: CC0

Related Links

https://github.com/fleschutz/PowerShell

Script Content

<#
.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
}

(generated by convert-ps2md.ps1 as of 11/20/2024 11:52:00)