forked from fleschutz/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist-dir-tree.ps1
executable file
·61 lines (57 loc) · 1.47 KB
/
list-dir-tree.ps1
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
<#
.SYNOPSIS
Lists a directory tree
.DESCRIPTION
This PowerShell script lists all files and folders in a directory tree.
.PARAMETER DirTree
Specifies the path to the directory tree
.EXAMPLE
PS> ./list-dir-tree C:\
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$DirTree = "$PWD")
function GetFileIcon { param([string]$Suffix)
switch ($Suffix) {
".csv" { return "📊" }
".epub" { return "📓" }
".gif" { return "🎨" }
".iso" { return "📀" }
".jpg" { return "🎨" }
".mp3" { return "🎵" }
".mkv" { return "🎬" }
default { return "📄" }
}
}
function ListDir { param([string]$Directory, [int]$Depth)
$Depth++
$Items = Get-ChildItem -path $Directory
foreach($Item in $Items) {
$Filename = $Item.Name
for ($i = 1; $i -lt $Depth; $i++) { Write-Host "│ " -noNewline }
if ($Item.Mode -like "d*") {
Write-Host "├" -noNewline
Write-Host -foregroundColor green "📂$Filename"
ListDir "$Directory\$Filename" $Depth
$global:Dirs++
} else {
$Icon = GetFileIcon $Item.Extension
Write-Host "├$($Icon)$Filename ($($Item.Length) bytes)"
$global:Files++
$global:Bytes += $Item.Length
}
}
}
try {
[int]$global:Dirs = 1
[int]$global:Files = 0
[int]$global:Bytes = 0
ListDir $DirTree 0
"(Total: $($global:Dirs) folders, $($global:Files) files, $($global:Bytes) bytes)"
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}