PowerShell/docs/convert-dir2zip.md

92 lines
2.4 KiB
Markdown
Raw Normal View History

2024-11-08 12:38:20 +01:00
The *convert-dir2zip.ps1* Script
===========================
2024-08-15 09:51:46 +02:00
This PowerShell script creates a new compressed .ZIP file from a directory (including subfolders).
Parameters
----------
```powershell
2024-11-08 12:35:11 +01:00
/home/markus/Repos/PowerShell/scripts/convert-dir2zip.ps1 [[-dirPath] <String>] [[-zipPath] <String>] [<CommonParameters>]
2024-08-15 09:51:46 +02:00
-dirPath <String>
Specifies the path to the directory
Required? false
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters? false
-zipPath <String>
Specifies the path to the target .ZIP file (default is dirPath.zip)
Required? false
Position? 2
Default value
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> ./convert-dir2zip.ps1 C:\Windows Win.zip
2024-11-08 12:35:11 +01:00
✅ Converted into compressed Win.zip in 291s.
2024-08-15 09:51:46 +02:00
```
Notes
-----
Author: Markus Fleschutz | License: CC0
Related Links
-------------
https://github.com/fleschutz/PowerShell
Script Content
--------------
```powershell
<#
.SYNOPSIS
Converts a directory into a compressed .ZIP file
.DESCRIPTION
This PowerShell script creates a new compressed .ZIP file from a directory (including subfolders).
.PARAMETER dirPath
Specifies the path to the directory
.PARAMETER zipPath
Specifies the path to the target .ZIP file (default is dirPath.zip)
.EXAMPLE
PS> ./convert-dir2zip.ps1 C:\Windows Win.zip
2024-11-08 12:35:11 +01:00
✅ Converted into compressed Win.zip in 291s.
2024-08-15 09:51:46 +02:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$dirPath = "", [string]$zipPath = "")
try {
if ($dirPath -eq "" ) { $dirPath = Read-Host "Enter the path to the folder" }
$StopWatch = [system.diagnostics.stopwatch]::startNew()
$dirPath = Resolve-Path $dirPath
if ($zipPath -eq "" ) { $zipPath = "$dirPath.zip" }
Compress-Archive -path $dirPath -destinationPath $zipPath
[int]$elapsed = $StopWatch.Elapsed.TotalSeconds
2024-11-08 12:35:11 +01:00
"✅ Converted into compressed $zipPath in $($elapsed)s."
2024-08-15 09:51:46 +02:00
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
```
2024-11-08 12:40:31 +01:00
*(generated by convert-ps2md.ps1 as of 11/08/2024 12:40:16)*