PowerShell/Scripts/list-dir-tree.ps1

45 lines
1.1 KiB
PowerShell
Raw Normal View History

2021-04-16 17:18:50 +02:00
#!/usr/bin/pwsh
2021-01-29 11:07:01 +01:00
<#
2021-04-16 17:22:26 +02:00
.SYNTAX list-dir-tree.ps1 [<dir-tree>]
.DESCRIPTION lists a directory tree
2021-03-22 20:10:18 +01:00
.LINK https://github.com/fleschutz/PowerShell
.NOTES Author: Markus Fleschutz / License: CC0
2021-01-29 11:07:01 +01:00
#>
2021-04-16 17:22:26 +02:00
param($DirTree = "$PWD")
2021-01-29 11:07:01 +01:00
2021-04-16 17:22:26 +02:00
function ListDir { param([string]$Directory, [int]$Depth)
2021-01-29 11:07:01 +01:00
$Depth++
$Items = get-childItem -path $Directory
foreach ($Item in $Items) {
$Filename = $Item.Name
2021-02-06 15:14:47 +01:00
if ($Item.Mode -like "d*") {
2021-01-29 11:07:01 +01:00
for ($i = 0; $i -lt $Depth; $i++) {
write-host -nonewline "+--"
}
2021-04-16 17:18:50 +02:00
write-host -foregroundColor green "📂$Filename"
2021-04-16 17:23:18 +02:00
ListDir "$Directory\$Filename" $Depth
2021-04-16 17:27:13 +02:00
$global:Dirs++
2021-01-29 11:07:01 +01:00
} else {
for ($i = 1; $i -lt $Depth; $i++) {
write-host -nonewline "| "
}
2021-02-06 15:14:47 +01:00
write-host "|-$Filename ($($Item.Length) bytes)"
2021-04-16 17:27:13 +02:00
$global:Files++
$global:Bytes += $Item.Length
2021-01-29 11:07:01 +01:00
}
}
}
try {
2021-04-16 17:27:13 +02:00
[int]$global:Dirs = 1
[int]$global:Files = 0
[int]$global:Bytes = 0
2021-04-16 17:22:26 +02:00
ListDir $DirTree 0
2021-04-16 17:27:13 +02:00
write-host "($($global:Dirs) directories, $($global:Files) files, $($global:Bytes) bytes total)"
2021-01-29 11:07:01 +01:00
exit 0
} catch {
2021-02-16 10:03:20 +01:00
write-error "ERROR: line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2021-01-29 11:07:01 +01:00
exit 1
}