-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
common.ps1
62 lines (48 loc) · 1.65 KB
/
common.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
# Common functions used by the other scripts
function Check-DirectoryExists {
param (
[string]$rootDirectory
)
return Test-Path -Path $rootDirectory -PathType Container
}
function Find-ReplaceInFiles {
param (
[string]$rootDirectory,
[string]$sourceString,
[string]$targetString
)
Get-ChildItem -Recurse -File $rootDirectory | ForEach-Object {
$filePath = $_.FullName
Find-ReplaceInFile -filePath $filePath -sourceString $sourceString -targetString $targetString
}
Write-Host "String replacement completed."
}
function Find-ReplaceInFile {
param (
[string]$filePath,
[string]$sourceString,
[string]$targetString
)
# Get the original encoding of the file
$sr = New-Object System.IO.StreamReader($filePath, $true)
[char[]] $buffer = new-object char[] 3
$sr.Read($buffer, 0, 3)
$originalEncoding = $sr.CurrentEncoding
$sr.Close()
# Making it simple as we only want to preserve UTF8 encoding
if ( $originalEncoding.BodyName -eq "utf-8" ) {
$fileEncoding = "UTF8"
}
else {
$fileEncoding = "ASCII"
}
# Read the content of the file using the original encoding
$content = Get-Content -Path $filePath -Raw -Encoding $fileEncoding
# Perform the replacement
$newContent = $content -replace [regex]::Escape($sourceString), $targetString
if ($content -ne $newContent) {
Write-Host "Replacing in $filePath"
# Save the modified content back to the file using the original encoding
Set-Content -Path $filePath -Value $newContent -Encoding $fileEncoding
}
}