PowerShell/Scripts/list-dir-tree.ps1

64 lines
1.5 KiB
PowerShell
Raw Normal View History

2023-04-27 15:22:28 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
2023-01-06 22:15:14 +01:00
Lists a directory tree
2021-10-04 21:29:23 +02:00
.DESCRIPTION
2022-09-10 13:46:57 +02:00
This PowerShell script lists all files and folders in a directory tree.
2021-10-16 13:40:20 +02:00
.PARAMETER DirTree
Specifies the path to the directory tree
2021-07-13 21:10:02 +02:00
.EXAMPLE
2023-08-06 21:35:36 +02:00
PS> ./list-dir-tree.ps1 C:\
2021-07-13 21:10:02 +02:00
.LINK
https://github.com/fleschutz/PowerShell
2022-01-29 12:47:46 +01:00
.NOTES
2022-09-06 21:42:04 +02:00
Author: Markus Fleschutz | License: CC0
2021-01-29 11:07:01 +01:00
#>
2021-07-15 15:51:22 +02:00
param([string]$DirTree = "$PWD")
2021-01-29 11:07:01 +01:00
2023-01-09 13:48:04 +01:00
function GetFileIcon { param([string]$Suffix)
switch ($Suffix) {
".csv" {return "📊"}
".epub" {return "📓"}
".exe" {return "⚙️"}
".gif" {return "📸"}
".iso" {return "📀"}
".jpg" {return "📸"}
".mp3" {return "🎵"}
".mkv" {return "🎬"}
".zip" {return "🎁"}
default {return "📄"}
2023-01-09 13:48:04 +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++
2022-09-10 13:46:57 +02:00
$Items = Get-ChildItem -path $Directory
foreach($Item in $Items) {
2021-01-29 11:07:01 +01:00
$Filename = $Item.Name
2023-01-09 13:48:04 +01:00
for ($i = 1; $i -lt $Depth; $i++) { Write-Host "" -noNewline }
2021-02-06 15:14:47 +01:00
if ($Item.Mode -like "d*") {
2023-01-09 13:48:04 +01:00
Write-Host "" -noNewline
2022-09-10 13:46:57 +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 {
2023-01-09 13:48:04 +01:00
$Icon = GetFileIcon $Item.Extension
2023-01-06 22:15:14 +01:00
Write-Host "$($Icon)$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
" ($($global:Dirs) folders, $($global:Files) files, $($global:Bytes) bytes in total)"
2021-09-27 10:09:45 +02:00
exit 0 # success
2021-01-29 11:07:01 +01:00
} catch {
2022-04-13 12:06:32 +02:00
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2021-01-29 11:07:01 +01:00
exit 1
2023-04-27 15:22:28 +02:00
}