Update list-folder.ps1

This commit is contained in:
Markus Fleschutz 2023-04-26 16:40:17 +02:00
parent 0cb33ec99b
commit 6abf457fb3

View File

@ -1,36 +1,47 @@
<# <#
.SYNOPSIS .SYNOPSIS
Lists the folder content Lists a folder
.DESCRIPTION .DESCRIPTION
This PowerShell script lists the directory content formatted in columns. This PowerShell script lists the content of a directory (alphabetically formatted in columns).
.PARAMETER SearchPattern .PARAMETER SearchPattern
Specifies the search pattern, "*" by default (means anything) Specifies the search pattern ("*" by default which means anything)
.EXAMPLE .EXAMPLE
PS> ./list-folder C:\ PS> ./list-folder C:\*
.LINK .LINK
https://github.com/fleschutz/PowerShell https://github.com/fleschutz/PowerShell
.NOTES .NOTES
Author: Markus Fleschutz | License: CC0 Author: Markus Fleschutz | License: CC0
#> #>
param([string]$SearchPattern = "*") param([string]$searchPattern = "*")
function ListFolder { param([string]$SearchPattern) function GetFileIcon { param([string]$suffix)
$Items = get-childItem -path "$SearchPattern" switch ($suffix) {
foreach ($Item in $Items) { ".csv" { return "📊" }
$Name = $Item.Name ".epub" { return "📓" }
if ($Name[0] -eq '.') { continue } # hidden file/dir ".gif" { return "🎨" }
if ($Item.Mode -like "d*") { $Icon = "📂" ".iso" { return "📀" }
} elseif ($Name -like "*.iso") { $Icon = "📀" ".jpg" { return "🎨" }
} elseif ($Name -like "*.mp3") { $Icon = "🎵" ".mp3" { return "🎵" }
} elseif ($Name -like "*.epub") { $Icon = "📓" ".mp4" { return "🎬" }
} else { $Icon = "📄" } ".mkv" { return "🎬" }
new-object PSObject -Property @{ Name = "$Icon$Name" } ".ps1" { return "📑" }
default { return "📄" }
}
}
function ListFolder { param([string]$searchPattern)
$items = Get-ChildItem -path "$searchPattern"
foreach ($item in $items) {
$name = $item.Name
if ($name[0] -eq '.') { continue } # hidden file/dir
if ($item.Mode -like "d*") { $icon = "📂" } else { $icon = GetFileIcon $item.Extension }
New-Object PSObject -property @{ Name = "$icon$name" }
} }
} }
try { try {
ListFolder $SearchPattern | format-wide -autoSize ListFolder $searchPattern | Format-Wide -autoSize
exit 0 # success exit 0 # success
} catch { } catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"