mirror of
https://github.com/fleschutz/PowerShell.git
synced 2025-04-09 18:19:32 +02:00
Rename folder Data to data
This commit is contained in:
parent
893273739a
commit
35e940a00c
@ -1,13 +1,13 @@
|
|||||||
<#
|
<#
|
||||||
.SYNOPSIS
|
.SYNOPSIS
|
||||||
Search and replace a pattern in the given files by the replacement
|
Search and replace a pattern in the given files by the replacement
|
||||||
.DESCRIPTION
|
.DESCRIPTION
|
||||||
This PowerShell script searches and replaces a pattern in the given files by the replacement.
|
This PowerShell script searches and replaces a pattern in the given files by the replacement.
|
||||||
.PARAMETER pattern
|
.PARAMETER pattern
|
||||||
Specifies the pattern to look for
|
Specifies the text pattern to look for
|
||||||
.PARAMETER replacement
|
.PARAMETER replacement
|
||||||
Specifies the replacement
|
Specifies the text replacement
|
||||||
.PARAMETER files
|
.PARAMETER filePattern
|
||||||
Specifies the file to scan
|
Specifies the file to scan
|
||||||
.EXAMPLE
|
.EXAMPLE
|
||||||
PS> ./replace-in-files NSA "No Such Agency" C:\Temp\*.txt
|
PS> ./replace-in-files NSA "No Such Agency" C:\Temp\*.txt
|
||||||
@ -17,28 +17,27 @@
|
|||||||
Author: Markus Fleschutz | License: CC0
|
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]::WriteAllText($path,
|
||||||
([System.IO.File]::ReadAllText($FilePath) -replace $Pattern, $Replacement)
|
([System.IO.File]::ReadAllText($path) -replace $pattern, $replacement)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if ($pattern -eq "" ) { $pattern = read-host "Enter search pattern" }
|
if ($pattern -eq "" ) { $pattern = Read-Host "Enter the text pattern to look for" }
|
||||||
if ($replacement -eq "" ) { $replacement = read-host "Enter replacement" }
|
if ($replacement -eq "" ) { $replacement = Read-Host "Enter the text replacement" }
|
||||||
if ($files -eq "" ) { $files = read-host "Enter files" }
|
if ($filePattern -eq "" ) { $filePattern = Read-Host "Enter the file pattern" }
|
||||||
|
|
||||||
$StopWatch = [system.diagnostics.stopwatch]::startNew()
|
$stopWatch = [system.diagnostics.stopwatch]::startNew()
|
||||||
|
$files = (Get-ChildItem -path "$filePattern" -attributes !Directory)
|
||||||
$fileList = (get-childItem -path "$files" -attributes !Directory)
|
foreach($file in $files) {
|
||||||
foreach($file in $fileList) {
|
|
||||||
ReplaceInFile $file $pattern $replacement
|
ReplaceInFile $file $pattern $replacement
|
||||||
}
|
}
|
||||||
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
|
[int]$elapsed = $stopWatch.Elapsed.TotalSeconds
|
||||||
"OK, replaced '$pattern' by '$replacement' in $($fileList.Count) files in $Elapsed sec."
|
"✔️ Replaced '$pattern' by '$replacement' in $($files.Count) files in $elapsed sec"
|
||||||
exit 0 # success
|
exit 0 # success
|
||||||
} catch {
|
} catch {
|
||||||
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
||||||
|
Can't render this file because it is too large.
|
Can't render this file because it is too large.
|
Can't render this file because it is too large.
|
Can't render this file because it has a wrong number of fields in line 8.
|
Can't render this file because it is too large.
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user