Skip to content

Instantly share code, notes, and snippets.

@lcloss
Last active July 20, 2023 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lcloss/a8f81ef5c34ab4bbe4606d2a2a1915c1 to your computer and use it in GitHub Desktop.
Save lcloss/a8f81ef5c34ab4bbe4606d2a2a1915c1 to your computer and use it in GitHub Desktop.
Compressing and Extracting folders
#
# Uses 7Z to compress all $targetPath's subfolders.
#
# Example:
# root
# - HTML
# - Folder1
# - Folder2
# - Folder3
#
# At root path, type:
#
# compressing HTML
# > It will produces Folder1.zip, Folder2.zip and Folder3.zip under HTML folder.
#
param([String]$targetPath)
$thisFolder = Get-Location
$logFile = "$($thisFolder)\compressing-errors.log"
Function LogWrite
{
Param ([string]$logString)
Add-content $LogFile -value $logString
}
$folders = Get-ChildItem -Path $targetPath -Directory -Force -ErrorAction SilentlyContinue | Select-Object FullName, Name
foreach($folder in $folders) {
try {
Write-Host
Write-Host "Compress folder $($folder.Name)..."
7Z a "$($targetPath)\$($folder.Name).zip" -stl "$($folder.Fullname)"
Write-Host "Folder compressed." -Fore Green
} catch {
Write-Host "Error on compressing $($folder.Fullname) !" -Fore Red
LogWrite "Erros on compressing $($folder.Fullname) !"
}
}
# ----------------
# Expand all compressed files contained in $targetPath
# Warning: this will expand current .zip or .7z to destiny folder, without create a subfolder.
# To create a subfolder with same name of compressed file, use extracting.ps1
# ----------------
param([String]$targetPath)
$thisFolder = Get-Location
$destPath = $targetPath
$logFile = "$($thisFolder)\expanding-errors.log"
Function LogWrite
{
Param ([string]$logString)
Add-content $LogFile -value $logString
}
$zipped = Get-ChildItem -Path "$($targetPath)\*" -File -Include *.zip, *.7z | Select-Object FullName, Name
foreach($zipfile in $zipped) {
try {
Write-Host
Write-Host "Expanding folder $($zipfile.Name)..."
7z e $zipfile.Fullname "-o$($destPath)"
Write-Host "Pasta descompactada." -Fore Green
} catch {
Write-Host "Error on expand folder $($zipfile.Fullname) on $($destPath) !" -Fore Red
LogWrite "Error on expand folder $($zipfile.Fullname) on $($destPath) !"
}
}
# ----------------
# Extract all compressed files contained in $targetPath
# Warning: this will expand current .zip or .7z to same name subfolder.
# To extract without create a subfolder with same name of compressed file, use expanding.ps1
# ----------------
param([String]$targetPath)
$thisFolder = Get-Location
$destPath = $targetPath
$logFile = "$($thisFolder)\extracting-errors.log"
Function LogWrite
{
Param ([string]$logString)
Add-content $LogFile -value $logString
}
$zipped = Get-ChildItem -Path "$($targetPath)\*" -File -Include *.zip, *.7z | Select-Object FullName, Name, Basename
foreach($zipfile in $zipped) {
try {
Write-Host
Write-Host "Extracting folder $($zipfile.Name)..."
# Write-Host "Para a pasta $($destPath)\$($zipfile.Basename)..."
7z x $zipfile.Fullname "-o$($destPath)\$($zipfile.Basename)"
Write-Host "Folder extracted." -Fore Green
} catch {
Write-Host "Error on extract folder $($zipfile.Fullname) to $($destPath) !" -Fore Red
LogWrite "Error on extract folder $($zipfile.Fullname) to $($destPath) !"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment