mirror of
https://github.com/fleschutz/PowerShell.git
synced 2025-01-28 16:48:39 +01:00
Update list-dir-tree.ps1
This commit is contained in:
parent
141a1de976
commit
860b43149a
@ -2,21 +2,24 @@
|
|||||||
.SYNOPSIS
|
.SYNOPSIS
|
||||||
Lists a directory tree
|
Lists a directory tree
|
||||||
.DESCRIPTION
|
.DESCRIPTION
|
||||||
This PowerShell script lists all files and folders in a directory tree.
|
This PowerShell script lists all files and folders in a neat directory tree (including icon and size).
|
||||||
.PARAMETER DirTree
|
.PARAMETER Path
|
||||||
Specifies the path to the directory tree
|
Specifies the path to the directory tree
|
||||||
.EXAMPLE
|
.EXAMPLE
|
||||||
PS> ./list-dir-tree.ps1 C:\
|
PS> ./list-dir-tree.ps1 C:\MyFolder
|
||||||
|
├📂Results
|
||||||
|
│ ├📄sales.txt (442K)
|
||||||
|
(2 folders, 1 file, 442K total)
|
||||||
.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]$DirTree = "$PWD")
|
param([string]$Path = "$PWD")
|
||||||
|
|
||||||
function GetFileIcon { param([string]$Suffix)
|
function GetFileIcon([string]$suffix) {
|
||||||
switch ($Suffix) {
|
switch ($suffix) {
|
||||||
".csv" {return "📊"}
|
".csv" {return "📊"}
|
||||||
".epub" {return "📓"}
|
".epub" {return "📓"}
|
||||||
".exe" {return "⚙️"}
|
".exe" {return "⚙️"}
|
||||||
@ -30,32 +33,43 @@ function GetFileIcon { param([string]$Suffix)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function ListDir { param([string]$Directory, [int]$Depth)
|
function Bytes2String([int64]$bytes) {
|
||||||
$Depth++
|
if ($bytes -lt 1000) { return "$bytes bytes" }
|
||||||
$Items = Get-ChildItem -path $Directory
|
$bytes /= 1000
|
||||||
foreach($Item in $Items) {
|
if ($bytes -lt 1000) { return "$($bytes)K" }
|
||||||
$Filename = $Item.Name
|
$bytes /= 1000
|
||||||
for ($i = 1; $i -lt $Depth; $i++) { Write-Host "│ " -noNewline }
|
if ($bytes -lt 1000) { return "$($bytes)MB" }
|
||||||
if ($Item.Mode -like "d*") {
|
$bytes /= 1000
|
||||||
Write-Host "├" -noNewline
|
if ($bytes -lt 1000) { return "$($bytes)GB" }
|
||||||
Write-Host -foregroundColor green "📂$Filename"
|
$bytes /= 1000
|
||||||
ListDir "$Directory\$Filename" $Depth
|
return "$($Bytes)TB"
|
||||||
$global:Dirs++
|
}
|
||||||
|
|
||||||
|
function ListDirectory([string]$path, [int]$depth) {
|
||||||
|
$depth++
|
||||||
|
$items = Get-ChildItem -path $path
|
||||||
|
foreach($item in $items) {
|
||||||
|
$filename = $item.Name
|
||||||
|
for ($i = 1; $i -lt $depth; $i++) { Write-Host "│ " -noNewline }
|
||||||
|
if ($item.Mode -like "d*") {
|
||||||
|
Write-Output "├📂$Filename"
|
||||||
|
ListDirectory "$path\$filename" $depth
|
||||||
|
$global:folders++
|
||||||
} else {
|
} else {
|
||||||
$Icon = GetFileIcon $Item.Extension
|
$icon = GetFileIcon $item.Extension
|
||||||
Write-Host "├$($Icon)$Filename ($($Item.Length) bytes)"
|
Write-Output "├$($icon)$filename ($(Bytes2String $item.Length))"
|
||||||
$global:Files++
|
$global:files++
|
||||||
$global:Bytes += $Item.Length
|
$global:bytes += $item.Length
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
[int]$global:Dirs = 1
|
[int]$global:folders = 1
|
||||||
[int]$global:Files = 0
|
[int]$global:files = 0
|
||||||
[int]$global:Bytes = 0
|
[int]$global:bytes = 0
|
||||||
ListDir $DirTree 0
|
ListDirectory $Path 0
|
||||||
" ($($global:Dirs) folders, $($global:Files) files, $($global:Bytes) bytes in total)"
|
Write-Output " ($($global:folders) folders, $($global:files) files, $(Bytes2String $global:bytes) total)"
|
||||||
exit 0 # success
|
exit 0 # success
|
||||||
} catch {
|
} catch {
|
||||||
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
||||||
|
Loading…
Reference in New Issue
Block a user