mirror of
https://github.com/fleschutz/PowerShell.git
synced 2025-03-29 08:06:51 +01:00
Improve search-files.ps1
This commit is contained in:
parent
3fa508fa9c
commit
61e76dea6a
@ -1,8 +1,8 @@
|
|||||||
<#
|
<#
|
||||||
.SYNOPSIS
|
.SYNOPSIS
|
||||||
search-files.ps1 [<pattern>] [<path>]
|
search-files.ps1 [<pattern>] [<files>]
|
||||||
.DESCRIPTION
|
.DESCRIPTION
|
||||||
Searches for the given pattern in the given files
|
Searches for a pattern in the given files
|
||||||
.EXAMPLE
|
.EXAMPLE
|
||||||
PS> ./search-files UFO C:\Temp\*.txt
|
PS> ./search-files UFO C:\Temp\*.txt
|
||||||
.NOTES
|
.NOTES
|
||||||
@ -11,9 +11,9 @@
|
|||||||
https://github.com/fleschutz/PowerShell
|
https://github.com/fleschutz/PowerShell
|
||||||
#>
|
#>
|
||||||
|
|
||||||
param([string]$Pattern = "", [string]$Path = "")
|
param([string]$pattern = "", [string]$files = "")
|
||||||
|
|
||||||
function ListScripts { param([string]$Pattern, [string]$Path)
|
function ListLocations { param([string]$Pattern, [string]$Path)
|
||||||
$List = Select-String -Path $Path -Pattern "$Pattern"
|
$List = Select-String -Path $Path -Pattern "$Pattern"
|
||||||
foreach ($Item in $List) {
|
foreach ($Item in $List) {
|
||||||
New-Object PSObject -Property @{
|
New-Object PSObject -Property @{
|
||||||
@ -22,14 +22,14 @@ function ListScripts { param([string]$Pattern, [string]$Path)
|
|||||||
'Text' = "$($Item.Line)"
|
'Text' = "$($Item.Line)"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
write-output "(pattern found at $($List.Count) locations)"
|
write-output "(found $($List.Count) locations with pattern '$pattern')"
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if ($Pattern -eq "" ) { $Pattern = read-host "Enter search pattern" }
|
if ($pattern -eq "" ) { $pattern = read-host "Enter search pattern" }
|
||||||
if ($Path -eq "" ) { $Path = read-host "Enter path to files" }
|
if ($files -eq "" ) { $files = read-host "Enter path to files" }
|
||||||
|
|
||||||
ListScripts $Pattern $Path | format-table -property Path,Line,Text
|
ListLocations $pattern $files | format-table -property Path,Line,Text
|
||||||
exit 0
|
exit 0
|
||||||
} catch {
|
} catch {
|
||||||
"⚠️ Error: $($Error[0]) ($($MyInvocation.MyCommand.Name):$($_.InvocationInfo.ScriptLineNumber))"
|
"⚠️ Error: $($Error[0]) ($($MyInvocation.MyCommand.Name):$($_.InvocationInfo.ScriptLineNumber))"
|
||||||
|
Loading…
Reference in New Issue
Block a user