Skip to content

Commit

Permalink
Refactor for common logic
Browse files Browse the repository at this point in the history
  • Loading branch information
PrimeEagle committed Dec 17, 2023
1 parent fe6eba9 commit 66d5916
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 589 deletions.
142 changes: 142 additions & 0 deletions Directory-Search-Functions.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
$cacheFile = "D:\My Code\PowerShell Scripts\_xd-cache.txt"
$cacheLimit = 1000

function Update-Cache {
param (
[string]$NewPath
)

$cache = @()
if (Test-Path $cacheFile) {
$cache = Get-Content $cacheFile
}

$cache = $cache | Where-Object { $_ -ne $NewPath }
$cache = @($NewPath) + $cache

if ($cache.Count -gt $cacheLimit) {
$cache = $cache[0..($cacheLimit-1)]
}

$cache | Set-Content $cacheFile
}

function Find-Directory {
param (
[string]$SearchPath,
[switch]$SearchAllDrives,
[switch]$ParentSearch,
[switch]$ChildSearch
)

$switchCount = @($SearchAllDrives, $ParentSearch, $ChildSearch).Where({ $_ }).Count
if ($switchCount -gt 1) {
throw "Only one of -SearchAllDrives, -ParentSearch, or -ChildSearch can be used at a time."
}

if (-not (Test-Path $cacheFile)) {
New-Item -Path $cacheFile -ItemType File -Force | Out-Null
}

$cache = Get-Content $cacheFile

if ($ParentSearch) {
$currentPath = Get-Location
$parentPaths = while ($currentPath.Parent) {
$currentPath = $currentPath.Parent
$currentPath.Path
}

$match = $cache | Where-Object { $parentPaths -contains $_ -and $_ -like "*$SearchPath*" } | Select-Object -First 1
} elseif ($ChildSearch) {
$currentPath = (Get-Location).Path
$match = $cache | Where-Object { $_.StartsWith($currentPath) -and $_ -like "*$SearchPath*" } | Select-Object -First 1
} else {
$currentDrive = (Get-Location).Drive.Name + ":"
$cache = $cache | Where-Object { $_.StartsWith($currentDrive) }
$match = $cache | Where-Object { $_ -like "*$SearchPath*" } | Select-Object -First 1
}

if ($match -and (Test-Path $match)) {
Update-Cache $match
return $match
} elseif ($match) {
$cache = $cache | Where-Object { $_ -ne $match }
$cache | Set-Content $cacheFile
}

if ($ParentSearch) {
$currentPath = Get-Location
while ($currentPath.Parent) {
$currentPath = $currentPath.Parent
if ($currentPath.Path -like "*$SearchPath*") {
Update-Cache $currentPath.Path
return $currentPath.Path
}
}
} elseif ($ChildSearch) {
$queue = New-Object System.Collections.Generic.Queue[System.IO.DirectoryInfo]
$queue.Enqueue((Get-Location))

while ($queue.Count -gt 0) {
$current = $queue.Dequeue()
if ($current.FullName -like "*$SearchPath*") {
Update-Cache $current.FullName
return $current.FullName
}

try {
foreach ($subDir in $current.GetDirectories()) {
$queue.Enqueue($subDir)
}
}
catch { }
}
} elseif ($SearchAllDrives) {
$drivesToSearch = Get-PSDrive -PSProvider 'FileSystem' | Select-Object -ExpandProperty Name | Sort-Object
foreach ($drive in $drivesToSearch) {
$root = Get-Item -Path "$($drive):"
$queue = New-Object System.Collections.Generic.Queue[System.IO.DirectoryInfo]
$queue.Enqueue($root)

while ($queue.Count -gt 0) {
$current = $queue.Dequeue()
if ($current.FullName -like "*$SearchPath*") {
Update-Cache $current.FullName
return $current.FullName
}

try {
foreach ($subDir in $current.GetDirectories()) {
$queue.Enqueue($subDir)
}
}
catch { }
}
}
} else {
$drivesToSearch = if ($SearchAllDrives) { Get-PSDrive -PSProvider 'FileSystem' | Select-Object -ExpandProperty Name | Sort-Object | Where-Object { $_ -ne $currentDrive.Trim(':') } | ForEach-Object { "$_:" } } else { $currentDrive }
foreach ($drive in $drivesToSearch) {
$root = Get-Item -Path $drive
$queue = New-Object System.Collections.Generic.Queue[System.IO.DirectoryInfo]
$queue.Enqueue($root)

while ($queue.Count -gt 0) {
$current = $queue.Dequeue()
if ($current.FullName -like "*$SearchPath*") {
Update-Cache $current.FullName
return $current.FullName
}

try {
foreach ($subDir in $current.GetDirectories()) {
$queue.Enqueue($subDir)
}
}
catch { }
}
}
}

return $null
}
69 changes: 4 additions & 65 deletions x.ps1
Original file line number Diff line number Diff line change
@@ -1,73 +1,12 @@
param (
[string]$Path
[string]$Path,
[switch]$All
)

$cacheFile = "D:\My Code\PowerShell Scripts\_xd-cache.txt"
$cacheLimit = 1000
. .\Directory-Search-Functions.ps1

function Update-Cache {
param (
[string]$NewPath
)

$cache = @()
if (Test-Path $cacheFile) {
$cache = Get-Content $cacheFile
}

$cache = $cache | Where-Object { $_ -ne $NewPath }
$cache = @($NewPath) + $cache

if ($cache.Count -gt $cacheLimit) {
$cache = $cache[0..($cacheLimit-1)]
}

$cache | Set-Content $cacheFile
}

function Find-Directory {
param (
[string]$SearchPath
)

if (-not (Test-Path $cacheFile)) {
New-Item -Path $cacheFile -ItemType File -Force | Out-Null
}

$cache = Get-Content $cacheFile
$match = $cache | Where-Object { $_ -like "*$SearchPath*" } | Select-Object -First 1

if ($match) {
Update-Cache $match
return $match
}

$currentDrive = (Get-Location).Drive.Root

$queue = New-Object System.Collections.Generic.Queue[System.IO.DirectoryInfo]
$root = Get-Item -Path $currentDrive
$queue.Enqueue($root)

while ($queue.Count -gt 0) {
$current = $queue.Dequeue()
if ($current.FullName -like "*$SearchPath*") {
Update-Cache $current.FullName
return $current.FullName
}

try {
foreach ($subDir in $current.GetDirectories()) {
$queue.Enqueue($subDir)
}
}
catch {
}
}

return $null
}

$directory = Find-Directory -SearchPath $Path
$directory = Find-Directory -SearchPath $Path -SearchAllDrives:$All
if ($directory) {
Set-Location $directory
} else {
Expand Down
73 changes: 6 additions & 67 deletions xb.ps1
Original file line number Diff line number Diff line change
@@ -1,76 +1,15 @@
param (
[string]$Path
[string]$Path,
[switch]$All
)

$cacheFile = "D:\My Code\PowerShell Scripts\_xd-cache.txt"
$cacheLimit = 1000
. .\Directory-Search-Functions.ps1

function Update-Cache {
param (
[string]$NewPath
)

$cache = @()
if (Test-Path $cacheFile) {
$cache = Get-Content $cacheFile
}

$cache = $cache | Where-Object { $_ -ne $NewPath }
$cache = @($NewPath) + $cache

if ($cache.Count -gt $cacheLimit) {
$cache = $cache[0..($cacheLimit-1)]
}

$cache | Set-Content $cacheFile
}

function Find-Directory {
param (
[string]$SearchPath
)

if (-not (Test-Path $cacheFile)) {
New-Item -Path $cacheFile -ItemType File -Force | Out-Null
}

$cache = Get-Content $cacheFile
$match = $cache | Where-Object { $_ -like "*$SearchPath*" } | Select-Object -First 1

if ($match) {
Update-Cache $match
return $match
}

$currentDrive = (Get-Location).Drive.Root

$queue = New-Object System.Collections.Generic.Queue[System.IO.DirectoryInfo]
$root = Get-Item -Path $currentDrive
$queue.Enqueue($root)

while ($queue.Count -gt 0) {
$current = $queue.Dequeue()
if ($current.FullName -like "*$SearchPath*") {
Update-Cache $current.FullName
return $current.FullName
}

try {
foreach ($subDir in $current.GetDirectories()) {
$queue.Enqueue($subDir)
}
}
catch {
}
}

return $null
}

$directory = Find-Directory -SearchPath $Path
$directory = Find-Directory -SearchPath $Path -SearchAllDrives:$All
if ($directory) {
Set-Location $directory
Start-Process "explorer.exe" -ArgumentList $directory
Set-Location $directory
Start-Process "explorer.exe" -ArgumentList $directory
} else {
Write-Host "No matching directory found for '$Path'."
}
62 changes: 2 additions & 60 deletions xc.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,10 @@ param (
[string]$Path
)

$cacheFile = "D:\My Code\PowerShell Scripts\_xd-cache.txt"
$cacheLimit = 1000
. .\Directory-Search-Functions.ps1

function Update-Cache {
param (
[string]$NewPath
)

$cache = @()
if (Test-Path $cacheFile) {
$cache = Get-Content $cacheFile
}

$cache = $cache | Where-Object { $_ -ne $NewPath }
$cache = @($NewPath) + $cache

if ($cache.Count -gt $cacheLimit) {
$cache = $cache[0..($cacheLimit-1)]
}
$cache | Set-Content $cacheFile
}

function Find-Directory {
param (
[string]$SearchPath
)

if (-not (Test-Path $cacheFile)) {
New-Item -Path $cacheFile -ItemType File -Force | Out-Null
}

$currentDirectory = Get-Location

$cache = Get-Content $cacheFile
$match = $cache | Where-Object {
$_ -like "*\$SearchPath*" -and $_.StartsWith($currentDirectory.Path) -and $_ -ne $currentDirectory.Path
} | Select-Object -First 1

if ($match) {
Update-Cache $match
return $match
}

$queue = New-Object System.Collections.Generic.Queue[System.IO.DirectoryInfo]
$root = Get-Item -Path $currentDirectory
$queue.Enqueue($root)

while ($queue.Count -gt 0) {
$current = $queue.Dequeue()
foreach ($subDir in $current.GetDirectories()) {
if ($subDir.Name -like "*$SearchPath*") {
Update-Cache $subDir.FullName
return $subDir.FullName
}
$queue.Enqueue($subDir)
}
}

return $null
}

$directory = Find-Directory -SearchPath $Path
$directory = Find-Directory -SearchPath $Path -ChildSearch
if ($directory) {
Set-Location $directory
} else {
Expand Down
Loading

0 comments on commit 66d5916

Please sign in to comment.