X

script for zipping multiple folders in single command

script for zipping multiple folders in single command

 

# Set the path where folders are located
$path = "C:\Users\mschi\Desktop\youtube\zip_unzip"
 
# Validate the path
if (-Not (Test-Path $path)) {
    Write-Host "Invalid path! Please check and try again."
    exit
}
 
# Get all folders in the path
$folders = Get-ChildItem -Path $path -Directory
 
# Check if folders exist
if ($folders.Count -eq 0) {
    Write-Host "No folders found in: $path"
    exit
}
 
# Loop through and zip each folder
foreach ($folder in $folders) {
    $folderPath = Join-Path $path $folder.Name
    $zipFile = Join-Path $path ($folder.Name + ".zip")
 
    # Remove old zip if it exists
    if (Test-Path $zipFile) {
        Remove-Item $zipFile -Force
    }
 
    # Use Compress-Archive on the contents of the folder
    Compress-Archive -Path "$folderPath\*" -DestinationPath $zipFile -Force
 
    if (Test-Path $zipFile) {
        Write-Host "Zipped: $folderPath -> $zipFile"
    } else {
        Write-Host "Failed: $folderPath"
    }
}
 
Write-Host ""
Write-Host "Zipping process completed."

 

 

Top