2024-11-08 12:38:20 +01:00
|
|
|
The *list-dir-tree.ps1* Script
|
|
|
|
===========================
|
2021-11-08 21:36:42 +01:00
|
|
|
|
2023-09-01 17:53:03 +02:00
|
|
|
This PowerShell script lists all files and folders in a neat directory tree (including icon and size).
|
2021-11-08 21:36:42 +01:00
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Parameters
|
|
|
|
----------
|
2021-11-08 21:36:42 +01:00
|
|
|
```powershell
|
2024-11-08 12:35:11 +01:00
|
|
|
/home/markus/Repos/PowerShell/scripts/list-dir-tree.ps1 [[-path] <String>] [<CommonParameters>]
|
2021-11-08 21:36:42 +01:00
|
|
|
|
2024-11-08 12:35:11 +01:00
|
|
|
-path <String>
|
2021-11-08 21:36:42 +01:00
|
|
|
Specifies the path to the directory tree
|
|
|
|
|
|
|
|
Required? false
|
|
|
|
Position? 1
|
|
|
|
Default value "$PWD"
|
|
|
|
Accept pipeline input? false
|
|
|
|
Accept wildcard characters? false
|
|
|
|
|
|
|
|
[<CommonParameters>]
|
|
|
|
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
|
|
|
|
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
|
|
|
|
```
|
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Example
|
|
|
|
-------
|
2021-11-08 21:36:42 +01:00
|
|
|
```powershell
|
2023-09-01 17:53:03 +02:00
|
|
|
PS> ./list-dir-tree.ps1 C:\MyFolder
|
|
|
|
├📂Results
|
|
|
|
│ ├📄sales.txt (442K)
|
2023-10-19 08:12:00 +02:00
|
|
|
(2 folders, 1 file, 442K file size in total)
|
2021-11-08 21:36:42 +01:00
|
|
|
|
|
|
|
```
|
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Notes
|
|
|
|
-----
|
2022-11-17 19:46:02 +01:00
|
|
|
Author: Markus Fleschutz | License: CC0
|
2021-11-08 21:36:42 +01:00
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Related Links
|
|
|
|
-------------
|
2021-11-08 21:36:42 +01:00
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Script Content
|
|
|
|
--------------
|
2022-11-17 20:05:34 +01:00
|
|
|
```powershell
|
2022-11-17 20:02:26 +01:00
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
2024-11-08 12:35:11 +01:00
|
|
|
Lists a dir tree
|
2022-11-17 20:02:26 +01:00
|
|
|
.DESCRIPTION
|
2023-09-01 17:53:03 +02:00
|
|
|
This PowerShell script lists all files and folders in a neat directory tree (including icon and size).
|
2024-11-08 12:35:11 +01:00
|
|
|
.PARAMETER path
|
2022-11-17 20:02:26 +01:00
|
|
|
Specifies the path to the directory tree
|
|
|
|
.EXAMPLE
|
2023-09-01 17:53:03 +02:00
|
|
|
PS> ./list-dir-tree.ps1 C:\MyFolder
|
|
|
|
├📂Results
|
|
|
|
│ ├📄sales.txt (442K)
|
2023-10-19 08:12:00 +02:00
|
|
|
(2 folders, 1 file, 442K file size in total)
|
2022-11-17 20:02:26 +01:00
|
|
|
.LINK
|
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
.NOTES
|
|
|
|
Author: Markus Fleschutz | License: CC0
|
|
|
|
#>
|
|
|
|
|
2024-11-08 12:35:11 +01:00
|
|
|
param([string]$path = "$PWD")
|
2022-11-17 20:02:26 +01:00
|
|
|
|
2023-09-01 17:53:03 +02:00
|
|
|
function GetFileIcon([string]$suffix) {
|
|
|
|
switch ($suffix) {
|
2023-05-26 12:20:18 +02:00
|
|
|
".csv" {return "📊"}
|
|
|
|
".epub" {return "📓"}
|
|
|
|
".exe" {return "⚙️"}
|
|
|
|
".gif" {return "📸"}
|
|
|
|
".iso" {return "📀"}
|
|
|
|
".jpg" {return "📸"}
|
|
|
|
".mp3" {return "🎵"}
|
|
|
|
".mkv" {return "🎬"}
|
2024-11-08 12:35:11 +01:00
|
|
|
".png" {return "📸"}
|
|
|
|
".rar" {return "🎁"}
|
|
|
|
".tar" {return "🎁"}
|
2023-05-26 12:20:18 +02:00
|
|
|
".zip" {return "🎁"}
|
|
|
|
default {return "📄"}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-01 17:53:03 +02:00
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2024-11-08 12:35:11 +01:00
|
|
|
function ListDir([string]$path, [int]$depth) {
|
2023-09-01 17:53:03 +02:00
|
|
|
$depth++
|
|
|
|
$items = Get-ChildItem -path $path
|
|
|
|
foreach($item in $items) {
|
|
|
|
$filename = $item.Name
|
|
|
|
for ($i = 1; $i -lt $depth; $i++) { Write-Host "│ " -noNewline }
|
|
|
|
if ($item.Mode -like "d*") {
|
|
|
|
Write-Output "├📂$Filename"
|
2024-11-08 12:35:11 +01:00
|
|
|
ListDir "$path\$filename" $depth
|
2022-11-17 20:02:26 +01:00
|
|
|
} else {
|
2023-09-01 17:53:03 +02:00
|
|
|
$icon = GetFileIcon $item.Extension
|
|
|
|
Write-Output "├$($icon)$filename ($(Bytes2String $item.Length))"
|
|
|
|
$global:files++
|
|
|
|
$global:bytes += $item.Length
|
2022-11-17 20:02:26 +01:00
|
|
|
}
|
|
|
|
}
|
2024-11-08 12:35:11 +01:00
|
|
|
$global:folders++
|
2022-11-17 20:02:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2024-11-08 12:35:11 +01:00
|
|
|
[int64]$global:folders = 0
|
2023-10-19 08:12:00 +02:00
|
|
|
[int64]$global:files = 0
|
|
|
|
[int64]$global:bytes = 0
|
2024-11-08 12:35:11 +01:00
|
|
|
ListDir $path 0
|
|
|
|
Write-Output " ($($global:folders) folders, $($global:files) files, $(Bytes2String $global:bytes) total)"
|
2022-11-17 20:02:26 +01:00
|
|
|
exit 0 # success
|
|
|
|
} catch {
|
|
|
|
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
|
|
|
exit 1
|
|
|
|
}
|
2022-11-17 20:05:34 +01:00
|
|
|
```
|
2022-11-17 20:02:26 +01:00
|
|
|
|
2024-11-20 11:52:20 +01:00
|
|
|
*(generated by convert-ps2md.ps1 as of 11/20/2024 11:51:55)*
|