2023-07-29 09:55:25 +02:00
|
|
|
## Script: *list-dir-tree.ps1*
|
2021-11-08 21:36:42 +01:00
|
|
|
|
2022-11-17 19:46:02 +01:00
|
|
|
This PowerShell script lists all files and folders in a directory tree.
|
2021-11-08 21:36:42 +01:00
|
|
|
|
|
|
|
## Parameters
|
|
|
|
```powershell
|
2023-07-29 09:45:37 +02:00
|
|
|
list-dir-tree.ps1 [[-DirTree] <String>] [<CommonParameters>]
|
2021-11-08 21:36:42 +01:00
|
|
|
|
|
|
|
-DirTree <String>
|
|
|
|
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.
|
|
|
|
```
|
|
|
|
|
|
|
|
## Example
|
|
|
|
```powershell
|
|
|
|
PS> ./list-dir-tree C:\
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
## Notes
|
2022-11-17 19:46:02 +01:00
|
|
|
Author: Markus Fleschutz | License: CC0
|
2021-11-08 21:36:42 +01:00
|
|
|
|
|
|
|
## Related Links
|
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
|
2023-07-29 09:55:25 +02:00
|
|
|
## Script Content
|
2022-11-17 20:05:34 +01:00
|
|
|
```powershell
|
2022-11-17 20:02:26 +01:00
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
2023-05-26 12:20:18 +02:00
|
|
|
Lists a directory tree
|
2022-11-17 20:02:26 +01:00
|
|
|
.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")
|
|
|
|
|
2023-05-26 12:20:18 +02: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 "📄"}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-17 20:02:26 +01:00
|
|
|
function ListDir { param([string]$Directory, [int]$Depth)
|
|
|
|
$Depth++
|
|
|
|
$Items = Get-ChildItem -path $Directory
|
|
|
|
foreach($Item in $Items) {
|
|
|
|
$Filename = $Item.Name
|
2023-05-26 12:20:18 +02:00
|
|
|
for ($i = 1; $i -lt $Depth; $i++) { Write-Host "│ " -noNewline }
|
2022-11-17 20:02:26 +01:00
|
|
|
if ($Item.Mode -like "d*") {
|
2023-05-26 12:20:18 +02:00
|
|
|
Write-Host "├" -noNewline
|
2022-11-17 20:02:26 +01:00
|
|
|
Write-Host -foregroundColor green "📂$Filename"
|
|
|
|
ListDir "$Directory\$Filename" $Depth
|
|
|
|
$global:Dirs++
|
|
|
|
} else {
|
2023-05-26 12:20:18 +02:00
|
|
|
$Icon = GetFileIcon $Item.Extension
|
|
|
|
Write-Host "├$($Icon)$Filename ($($Item.Length) bytes)"
|
2022-11-17 20:02:26 +01:00
|
|
|
$global:Files++
|
|
|
|
$global:Bytes += $Item.Length
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
[int]$global:Dirs = 1
|
|
|
|
[int]$global:Files = 0
|
|
|
|
[int]$global:Bytes = 0
|
|
|
|
ListDir $DirTree 0
|
2023-05-26 12:20:18 +02:00
|
|
|
" ($($global:Dirs) folders, $($global:Files) files, $($global:Bytes) bytes in 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
|
|
|
|
2023-07-29 09:55:25 +02:00
|
|
|
*(generated by convert-ps2md.ps1 using the comment-based help of list-dir-tree.ps1 as of 07/29/2023 09:55:12)*
|