Merge branch 'master' of github.com:fleschutz/PowerShell

This commit is contained in:
Markus Fleschutz
2023-08-31 17:21:09 +02:00
5 changed files with 52 additions and 37 deletions

View File

@ -2,21 +2,24 @@
.SYNOPSIS
Lists a directory tree
.DESCRIPTION
This PowerShell script lists all files and folders in a directory tree.
.PARAMETER DirTree
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:\
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]$DirTree = "$PWD")
param([string]$Path = "$PWD")
function GetFileIcon { param([string]$Suffix)
switch ($Suffix) {
function GetFileIcon([string]$suffix) {
switch ($suffix) {
".csv" {return "📊"}
".epub" {return "📓"}
".exe" {return "⚙️"}
@ -30,32 +33,43 @@ function GetFileIcon { param([string]$Suffix)
}
}
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++
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-Host "$($Icon)$Filename ($($Item.Length) bytes)"
$global:Files++
$global:Bytes += $Item.Length
$icon = GetFileIcon $item.Extension
Write-Output "$($icon)$filename ($(Bytes2String $item.Length))"
$global:files++
$global:bytes += $item.Length
}
}
}
try {
[int]$global:Dirs = 1
[int]$global:Files = 0
[int]$global:Bytes = 0
ListDir $DirTree 0
" ($($global:Dirs) folders, $($global:Files) files, $($global:Bytes) bytes in total)"
[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])"

View File

@ -50,7 +50,7 @@ try {
if ($lastExitCode -ne "0") { throw "'git submodule update' failed with exit code $lastExitCode" }
[int]$elapsed = $stopWatch.Elapsed.TotalSeconds
"✔️ Creating branch '$newBranch' in repo 📂$repoPathName (based on '$currentBranch') took $elapsed sec"
"✔️ Created new branch '$newBranch' in repo 📂$repoPathName (based on '$currentBranch', it took $elapsed sec)"
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"

View File

@ -52,7 +52,7 @@ try {
if ($lastExitCode -ne "0") { throw "'git submodule update' failed with exit code $lastExitCode" }
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
"✔️ Switching repo 📂$RepoDirName to branch '$BranchName' took $Elapsed sec"
"✔️ Switched repo 📂$RepoDirName to branch '$BranchName' (in $Elapsed sec)"
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"

View File

@ -26,9 +26,9 @@ function GetRandomCodeLine {
0 { return $Tabs + "`$count = 0" }
1 { return $Tabs + "`$count++" }
2 { return $Tabs + "exit 0 # success" }
3 { return $Tabs + "`$Files = Get-ChildItem C:" }
3 { return $Tabs + "`$files = Get-ChildItem C:" }
4 { return $Tabs + "Start-Sleep -seconds 1" }
5 { return $Tabs + "`$Generator = New-Object System-Random" }
5 { return $Tabs + "`$generator = New-Object System-Random" }
6 { $global:Tabs = " "; return "} else {" }
7 { $global:Tabs = " "; return "} catch {" }
8 { $global:Tabs = " "; return "} elseif (`$count -eq 0) {" }
@ -39,21 +39,21 @@ function GetRandomCodeLine {
13 { return $Tabs + "return 1" }
14 { return $Tabs + "return 0" }
15 { return $Tabs + "Write-Progress `"Working...`" " }
16 { return $Tabs + "[bool]`$KeepAlive = `$true" }
16 { return $Tabs + "[bool]`$keepAlive = `$true" }
17 { return $Tabs + "# Copyright © 2023 write-code.ps1. All Rights Reserved." }
18 { $global:Tabs = " "; return "for ([int]`$i = 0; `$i -lt 42; `$i++) {" }
19 { return $Tabs + "`$StopWatch = [system.diagnostics.stopwatch]::startNew()" }
20 { return $Tabs + "[int]`$Elapsed = `$StopWatch.Elapsed.TotalSeconds" }
19 { return $Tabs + "`$stopWatch = [system.diagnostics.stopwatch]::startNew()" }
20 { return $Tabs + "[int]`$elapsed = `$stopWatch.Elapsed.TotalSeconds" }
21 { $global:Tabs = " "; return "if (`$count -eq 42) {" }
22 { $global:Tabs = " "; return "} finally {" }
23 { return $Tabs + "throw `"Can't open file`" " }
24 { return $Tabs + "Start-Sleep -milliseconds 42" }
25 { return $Tabs + "`$Choice = Read-Host `"Red or blue pill?`"" }
26 { return $Tabs + "[int]`$Answer = 42" }
26 { return $Tabs + "[int]`$answer = 42" }
27 { $global:Tabs = ""; return "}" }
28 { $global:Tabs = " "; return "try {" }
29 { $global:Tabs = " "; return "foreach(`$Row in `$Table) {" }
30 { $global:Tabs = " "; return "foreach(`$File in `$Files) {" }
29 { $global:Tabs = " "; return "foreach(`$row in `$table) {" }
30 { $global:Tabs = " "; return "foreach(`$file in `$files) {" }
}
}