This repository has been archived by the owner on Jul 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathUpdate-StringInFile.ps1
113 lines (94 loc) · 3.19 KB
/
Update-StringInFile.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
###############################################################################################################
# Language : PowerShell 4.0
# Filename : Update-StringInFile.ps1
# Autor : BornToBeRoot (https://github.com/BornToBeRoot)
# Description : Replace a string in multiple files
# Repository : https://github.com/BornToBeRoot/PowerShell
###############################################################################################################
<#
.SYNOPSIS
Replace a string in one or multiple files
.DESCRIPTION
Replace a string in one or multiple files.
Binary files (*.zip, *.exe, etc.) are not touched by this script.
.EXAMPLE
Update-StringInFile -Path E:\Temp\Files\ -Find "Test1" -Replace "Test2" -Verbose
VERBOSE: Binary files like (*.zip, *.exe, etc...) are ignored
VERBOSE: Total files with string to replace found: 3
VERBOSE: Current file: E:\Temp\Files\File_01.txt
VERBOSE: Number of strings to replace in current file: 1
VERBOSE: Current file: E:\Temp\Files\File_02.txt
VERBOSE: Number of strings to replace in current file: 1
VERBOSE: Current file: E:\Temp\Files\File_03.txt
VERBOSE: Number of strings to replace in current file: 2
.LINK
https://github.com/BornToBeRoot/PowerShell/blob/master/Documentation/Function/Update-StringInFile.README.md
#>
function Update-StringInFile
{
[CmdletBinding(SupportsShouldProcess=$true)]
Param(
[Parameter(
Position=0,
HelpMessage="Folder where the files are stored (will search recursive)")]
[ValidateScript({
if(Test-Path -Path $_)
{
return $true
}
else
{
throw "Enter a valid path!"
}
})]
[String]$Path = (Get-Location),
[Parameter(
Position=1,
Mandatory=$true,
HelpMessage="String to find")]
[String]$Find,
[Parameter(
Position=2,
Mandatory=$true,
HelpMessage="String to replace")]
[String]$Replace,
[Parameter(
Position=3,
HelpMessage="String must be case sensitive (Default=false)")]
[switch]$CaseSensitive=$false
)
Begin{
}
Process{
Write-Verbose -Message "Binary files like (*.zip, *.exe, etc...) are ignored"
$Files = Get-ChildItem -Path $Path -Recurse | Where-Object { ($_.PSIsContainer -eq $false) -and ((Test-IsFileBinary -FilePath $_.FullName) -eq $false) } | Select-String -Pattern ([regex]::Escape($Find)) -CaseSensitive:$CaseSensitive | Group-Object Path
Write-Verbose -Message "Total files with string to replace found: $($Files.Count)"
# Go through each file
foreach($File in $Files)
{
Write-Verbose -Message "File:`t$($File.Name)"
Write-Verbose -Message "Number of strings to replace in current file:`t$($File.Count)"
if($PSCmdlet.ShouldProcess($File.Name))
{
try
{
# Replace string
if($CaseSensitive)
{
(Get-Content -Path $File.Name) -creplace [regex]::Escape($Find), $Replace | Set-Content -Path $File.Name -Force
}
else
{
(Get-Content -Path $File.Name) -replace [regex]::Escape($Find), $Replace | Set-Content -Path $File.Name -Force
}
}
catch
{
Write-Error -Message "$($_.Exception.Message)" -Category InvalidData
}
}
}
}
End{
}
}