-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZipAllFolders.ps1
139 lines (123 loc) · 3.2 KB
/
ZipAllFolders.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<#
.SYNOPSIS
Zips all folders.
.DESCRIPTION
This script zips all the folders in the directory given in the first command-line argument.
(or the current working directory if the arg is left empty)
.PARAMETER Location
Specifies the path to zip all the folders in it.
#>
param (
[Parameter(Position = 0)]
[string]$Location = "."
)
Push-Location $Location
if (!$?) { exit 1 }
function Get-Choice {
<#
.SYNOPSIS
Get a yes/no choice from the user.
.PARAMETER Prompt
The question to display.
#>
[CmdletBinding()]
param (
[Parameter(Position = 0, Mandatory)]
[string]$Prompt
)
while ($true) {
Write-Host -NoNewline "$Prompt [Y,N] "
do {
$Choice = $Host.UI.RawUI.ReadKey()
} until ($Choice.Character)
Write-Host ""
switch ($Choice.Character) {
'Y' { return $true }
'N' { return $false }
default { Write-Host "Invalid input." }
}
}
}
function Pause-Host {
<#
.SYNOPSIS
Wait for keypress.
.PARAMETER Message
The message to display while waiting.
#>
[CmdletBinding()]
param (
[Parameter(Position = 0, Mandatory)]
[string]$Message
)
Write-Host -NoNewline $Message
do {
$Key = $Host.UI.RawUI.ReadKey("NoEcho, IncludeKeyDown")
} until ($Key.Character)
Write-Host ""
}
function Pretty-Size {
<#
.SYNOPSIS
Get human-readable size from bytes.
.PARAMETER Size
Size in bytes.
.PARAMETER Precision
Precision for the result.
#>
[CmdletBinding()]
param (
[Parameter(Position = 0, Mandatory, ValueFromPipeline)]
[uint64]$Size,
[Parameter()]
[uint32]$Precision = 2
)
begin {
[string]$Format = "{0:F${Precision}} {1}"
}
process {
switch ($Size) {
{ $PSItem -ge 1TB } { return $Format -f ($Size / 1TB), "TB" }
{ $PSItem -ge 1GB } { return $Format -f ($Size / 1GB), "GB" }
{ $PSItem -ge 1MB } { return $Format -f ($Size / 1MB), "MB" }
{ $PSItem -ge 1KB } { return $Format -f ($Size / 1KB), "KB" }
default { return "$Size B" }
}
}
}
[bool]$RmFolders = Get-Choice "Remove original folders?"
Write-Host ""
[int]$Count = 0
try {
Get-ChildItem -Directory |
ForEach-Object {
++$Count
Write-Host "Folder ${Count}: $($PSItem.Name)"
$ZipName = "$($PSItem.Name).zip"
Compress-Archive -CompressionLevel NoCompression `
-DestinationPath $ZipName `
-Path "$($PSItem.Name)\*" `
-Update
if (!(Test-Path $ZipName)) {
Write-Host "Folder is empty.`n"
return
}
Write-Host "Size ~ $($($(Get-Item $ZipName).Length) | Pretty-Size)"
if ($RmFolders) {
Remove-Item -Recurse -Force $PSItem.Name
Write-Host "Folder deleted."
}
Write-Host ""
}
}
catch {
Write-Host -ForegroundColor Red "Error occurred during the process:"
Write-Host $PSItem
exit 1
}
finally {
Pop-Location
}
Write-Host "Done."
Pause-Host "Press any key to continue . . . "
exit 0