mirror of
https://github.com/fleschutz/PowerShell.git
synced 2024-12-29 10:09:37 +01:00
126 lines
3.0 KiB
Markdown
126 lines
3.0 KiB
Markdown
*list-dir-tree.ps1*
|
|
================
|
|
|
|
This PowerShell script lists all files and folders in a neat directory tree (including icon and size).
|
|
|
|
Parameters
|
|
----------
|
|
```powershell
|
|
PS> ./list-dir-tree.ps1 [[-Path] <String>] [<CommonParameters>]
|
|
|
|
-Path <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.ps1 C:\MyFolder
|
|
├📂Results
|
|
│ ├📄sales.txt (442K)
|
|
(2 folders, 1 file, 442K total)
|
|
|
|
```
|
|
|
|
Notes
|
|
-----
|
|
Author: Markus Fleschutz | License: CC0
|
|
|
|
Related Links
|
|
-------------
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
Script Content
|
|
--------------
|
|
```powershell
|
|
<#
|
|
.SYNOPSIS
|
|
Lists a directory tree
|
|
.DESCRIPTION
|
|
This PowerShell script lists all files and folders in a neat directory tree (including icon and size).
|
|
.PARAMETER Path
|
|
Specifies the path to the directory tree
|
|
.EXAMPLE
|
|
PS> ./list-dir-tree.ps1 C:\MyFolder
|
|
├📂Results
|
|
│ ├📄sales.txt (442K)
|
|
(2 folders, 1 file, 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 "🎬"}
|
|
".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 ListDirectory([string]$path, [int]$depth) {
|
|
$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"
|
|
ListDirectory "$path\$filename" $depth
|
|
$global:folders++
|
|
} else {
|
|
$icon = GetFileIcon $item.Extension
|
|
Write-Output "├$($icon)$filename ($(Bytes2String $item.Length))"
|
|
$global:files++
|
|
$global:bytes += $item.Length
|
|
}
|
|
}
|
|
}
|
|
|
|
try {
|
|
[int]$global:folders = 1
|
|
[int]$global:files = 0
|
|
[int]$global:bytes = 0
|
|
ListDirectory $Path 0
|
|
Write-Output " ($($global:folders) folders, $($global:files) files, $(Bytes2String $global:bytes) total)"
|
|
exit 0 # success
|
|
} catch {
|
|
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
|
exit 1
|
|
}
|
|
```
|
|
|
|
*(generated by convert-ps2md.ps1 using the comment-based help of list-dir-tree.ps1 as of 09/01/2023 17:51:52)*
|