-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.filetypecounter.ps1
71 lines (60 loc) · 2.62 KB
/
fs.filetypecounter.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
[CmdletBinding(PositionalBinding = $false)]
param(
[Parameter()][switch] $Recurse,
[Parameter()][switch] $ObjectOutput,
[Parameter(Position = 0)]
[string] $Path = $PWD
)
if (($Path -eq '--help' -or $Path -eq '?') -or [string]::IsNullOrWhiteSpace($Path)) {
Write-Output "[fs.filetypecounter] Usage: fs.filetypecounter.ps1 [-Path] <PATH> [-Recurse] [-ObjectOutput]"
exit 0
}
if (-not (Test-Path -LiteralPath $Path -PathType Container)) {
Write-Output "[fs.filetypecounter] The given path parameter ""$Path"" does not exist or is not a valid path!"
exit 1
}
$Path = Convert-Path -LiteralPath $Path
if ($Recurse) {
$Full = Get-ChildItem -LiteralPath $Path -Recurse -Force -File
}
else {
$Full = Get-ChildItem -LiteralPath $Path -Force -File
}
$Size = ($Full | Measure-Object).Count
$Exts = [System.Collections.Generic.List[System.String]]::new()
$Outp = [System.Collections.Generic.List[System.Object]]::new()
foreach ($Item in $Full) {
if (-not ($Exts.Contains($Item.Extension))) {
$Exts.Add($Item.Extension)
}
}
foreach ($Item in $Exts) {
$Outp.Add([pscustomobject]@{Extension = $Item; Count = [UInt64] 0})
}
if ($Exts.Contains([string]::Empty)) {
$Outp[$Exts.IndexOf([string]::Empty)].Extension = '(No Extension)'
}
for ($i = 0; $i -lt $Size; $i++) {
$cext = $Full[$i].Extension
$aidx = $Exts.IndexOf($cext)
$cout = $Outp[$aidx].Count
$Outp[$aidx].Count = ++$cout
}
if ($ObjectOutput) {
return $Outp
}
else {
Write-Output '┌────────────────────────────────┬─────────────────────────────────┐'
Write-Output '│ Filetype (Extension) │ Amount │'
Write-Output '├────────────────────────────────┼─────────────────────────────────┤'
foreach ($Entry in $Outp) {
$lstr = [string]::Concat('│', $Entry.Extension.PadLeft(18), '│'.PadLeft(15))
$rstr = [string]::Concat(([string]$Entry.Count).PadLeft(18), '│'.PadLeft(16))
$ostr = [string]::Concat($lstr, $rstr)
Write-Output $ostr
}
$tstr = [string]::Concat('│', "Total Files: $Size".PadLeft(24), '│'.PadLeft(9))
Write-Output '├────────────────────────────────┼─────────────────────────────────┘'
Write-Output $tstr
Write-Output '└────────────────────────────────┘'
}