2021-01-29 11:07:01 +01:00
|
|
|
#!/snap/bin/powershell
|
|
|
|
<#
|
|
|
|
.SYNTAX ./visualize-dir-tree.ps1 [<dir-tree>]
|
|
|
|
.DESCRIPTION visualizes the given/current directory tree
|
|
|
|
.LINK https://github.com/fleschutz/PowerShell
|
|
|
|
.NOTES Author: Markus Fleschutz / License: CC0
|
|
|
|
#>
|
|
|
|
|
|
|
|
param([string]$DirTree = "")
|
|
|
|
|
|
|
|
function VisualizeDirectory { param([string]$Directory, [int]$Depth)
|
|
|
|
$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-02-06 15:14:47 +01:00
|
|
|
write-host -foregroundColor green "$Filename/"
|
2021-01-29 11:07:01 +01:00
|
|
|
VisualizeDirectory "$Directory\$Filename" $Depth
|
2021-02-06 15:14:47 +01:00
|
|
|
$global:NumDirs++
|
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)"
|
|
|
|
$global:NumBytes += $Item.Length
|
2021-01-29 11:07:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if ($DirTree -eq "") {
|
|
|
|
$DirTree = "$PWD"
|
|
|
|
}
|
2021-02-06 15:14:47 +01:00
|
|
|
$global:NumDirs = 1
|
|
|
|
$global:NumBytes = 0
|
2021-01-29 11:07:01 +01:00
|
|
|
VisualizeDirectory $DirTree 0
|
2021-02-06 15:14:47 +01:00
|
|
|
write-host "($($global:NumDirs) dirs, $($global:NumBytes) bytes total)"
|
2021-01-29 11:07:01 +01:00
|
|
|
exit 0
|
|
|
|
} catch {
|
|
|
|
write-error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
|
|
|
exit 1
|
|
|
|
}
|