mirror of
https://github.com/fleschutz/PowerShell.git
synced 2024-11-21 23:43:25 +01:00
Rename folder Data to data
This commit is contained in:
parent
893273739a
commit
35e940a00c
@ -1,13 +1,13 @@
|
||||
<#
|
||||
<#
|
||||
.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 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
|
||||
@ -17,28 +17,27 @@
|
||||
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])"
|
||||
|
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