Last active
July 20, 2023 08:36
-
-
Save lcloss/a8f81ef5c34ab4bbe4606d2a2a1915c1 to your computer and use it in GitHub Desktop.
Compressing and Extracting folders
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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) !" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ---------------- | |
# 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) !" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ---------------- | |
# 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