mirror of
https://github.com/fleschutz/PowerShell.git
synced 2025-08-10 21:33:47 +02:00
Updated the Markdown manuals
This commit is contained in:
@ -6,10 +6,10 @@ This PowerShell script searches and replaces a pattern in the given files by the
|
||||
Parameters
|
||||
----------
|
||||
```powershell
|
||||
PS> ./replace-in-files.ps1 [[-pattern] <String>] [[-replacement] <String>] [[-files] <String>] [<CommonParameters>]
|
||||
PS> ./replace-in-files.ps1 [[-pattern] <String>] [[-replacement] <String>] [[-filePattern] <String>] [<CommonParameters>]
|
||||
|
||||
-pattern <String>
|
||||
Specifies the pattern to look for
|
||||
Specifies the text pattern to look for
|
||||
|
||||
Required? false
|
||||
Position? 1
|
||||
@ -18,7 +18,7 @@ PS> ./replace-in-files.ps1 [[-pattern] <String>] [[-replacement] <String>] [[-fi
|
||||
Accept wildcard characters? false
|
||||
|
||||
-replacement <String>
|
||||
Specifies the replacement
|
||||
Specifies the text replacement
|
||||
|
||||
Required? false
|
||||
Position? 2
|
||||
@ -26,7 +26,7 @@ PS> ./replace-in-files.ps1 [[-pattern] <String>] [[-replacement] <String>] [[-fi
|
||||
Accept pipeline input? false
|
||||
Accept wildcard characters? false
|
||||
|
||||
-files <String>
|
||||
-filePattern <String>
|
||||
Specifies the file to scan
|
||||
|
||||
Required? false
|
||||
@ -64,10 +64,10 @@ Script Content
|
||||
.DESCRIPTION
|
||||
This PowerShell script searches and replaces a pattern in the given files by the replacement.
|
||||
.PARAMETER pattern
|
||||
Specifies the pattern to look for
|
||||
Specifies the text pattern to look for
|
||||
.PARAMETER replacement
|
||||
Specifies the replacement
|
||||
.PARAMETER files
|
||||
Specifies the text replacement
|
||||
.PARAMETER filePattern
|
||||
Specifies the file to scan
|
||||
.EXAMPLE
|
||||
PS> ./replace-in-files NSA "No Such Agency" C:\Temp\*.txt
|
||||
@ -77,28 +77,27 @@ Script Content
|
||||
Author: Markus Fleschutz | License: CC0
|
||||
#>
|
||||
|
||||
param([string]$pattern = "", [string]$replacement = "", [string]$files = "")
|
||||
param([string]$pattern = "", [string]$replacement = "", [string]$filePattern = "")
|
||||
|
||||
function ReplaceInFile { param([string]$FilePath, [string]$Pattern, [string]$Replacement)
|
||||
function ReplaceInFile { param([string]$path, [string]$pattern, [string]$replacement)
|
||||
|
||||
[System.IO.File]::WriteAllText($FilePath,
|
||||
([System.IO.File]::ReadAllText($FilePath) -replace $Pattern, $Replacement)
|
||||
[System.IO.File]::WriteAllText($path,
|
||||
([System.IO.File]::ReadAllText($path) -replace $pattern, $replacement)
|
||||
)
|
||||
}
|
||||
|
||||
try {
|
||||
if ($pattern -eq "" ) { $pattern = read-host "Enter search pattern" }
|
||||
if ($replacement -eq "" ) { $replacement = read-host "Enter replacement" }
|
||||
if ($files -eq "" ) { $files = read-host "Enter files" }
|
||||
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" }
|
||||
|
||||
$StopWatch = [system.diagnostics.stopwatch]::startNew()
|
||||
|
||||
$fileList = (get-childItem -path "$files" -attributes !Directory)
|
||||
foreach($file in $fileList) {
|
||||
$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
|
||||
"OK, replaced '$pattern' by '$replacement' in $($fileList.Count) files in $Elapsed sec."
|
||||
[int]$elapsed = $stopWatch.Elapsed.TotalSeconds
|
||||
"✔️ Replaced '$pattern' by '$replacement' in $($files.Count) files in $elapsed sec"
|
||||
exit 0 # success
|
||||
} catch {
|
||||
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
||||
@ -106,4 +105,4 @@ try {
|
||||
}
|
||||
```
|
||||
|
||||
*(generated by convert-ps2md.ps1 using the comment-based help of replace-in-files.ps1 as of 10/19/2023 08:11:42)*
|
||||
*(generated by convert-ps2md.ps1 using the comment-based help of replace-in-files.ps1 as of 12/07/2023 20:24:22)*
|
||||
|
Reference in New Issue
Block a user