-
-
Notifications
You must be signed in to change notification settings - Fork 544
Expand file tree
/
Copy pathlist-dir-tree.ps1
More file actions
executable file
·85 lines (80 loc) · 2.31 KB
/
list-dir-tree.ps1
File metadata and controls
executable file
·85 lines (80 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<#
.SYNOPSIS
Lists a directory tree
.DESCRIPTION
This PowerShell script lists all files and folders in a directory tree (including icon and size).
.PARAMETER path
Specifies the file path to the directory tree
.EXAMPLE
PS> ./list-dir-tree.ps1 C:\MyFolder
├📂Results
│ ├📄sales.txt (442K)
(1 file, 2 folders, 1 folder level, 442K total)
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$path = "$PWD")
function GetFileIcon([string]$suffix) {
switch ($suffix) {
".csv" {return "📊"}
".epub" {return "📓"}
".exe" {return "⚙️"}
".gif" {return "📸"}
".iso" {return "📀"}
".jpg" {return "📸"}
".mp3" {return "🎵"}
".mkv" {return "🎬"}
".png" {return "📸"}
".rar" {return "🎁"}
".tar" {return "🎁"}
".zip" {return "🎁"}
default {return "📄"}
}
}
function Bytes2String([int64]$bytes) {
if ($bytes -lt 1000) { return "$bytes bytes" }
$bytes /= 1000
if ($bytes -lt 1000) { return "$($bytes)K" }
$bytes /= 1000
if ($bytes -lt 1000) { return "$($bytes)MB" }
$bytes /= 1000
if ($bytes -lt 1000) { return "$($bytes)GB" }
$bytes /= 1000
return "$($Bytes)TB"
}
function ListDir([string]$path, [int]$depth) {
$items = Get-ChildItem -path $path
# files first
foreach($item in $items) {
if ($item.Mode -notlike "d*") {
Write-Host " " -noNewline
for ([int]$i = 1; $i -lt $depth; $i++) { Write-Host " " -noNewline }
Write-Host "├$(GetFileIcon $item.Extension)$($item.Name) ($(Bytes2String $item.Length))"
$global:files++
$global:bytes += $item.Length
}
}
# folders second
foreach($item in $items) {
if ($item.Mode -like "d*") {
Write-Host " " -noNewline
for ([int]$i = 1; $i -lt $depth; $i++) { Write-Host " " -noNewline }
Write-Host "├📂$($item.Name)"
ListDir "$path\$($item.Name)" ($depth + 1)
}
}
$global:folders++
if ($depth -gt $global:depth) { $global:depth = $depth }
}
try {
Write-Host "`n 📂$path"
[int64]$global:files = $global:folders = $global:depth = $global:bytes = 0
ListDir $path 1
Write-Host "($($global:files) files, $($global:folders) folders, $($global:depth) folder levels, $(Bytes2String $global:bytes) total)"
exit 0 # success
} catch {
"⚠️ ERROR: $($Error[0]) (script line $($_.InvocationInfo.ScriptLineNumber))"
exit 1
}