script for zipping multiple folders in single command ( Including Empty Folders )
# Set the path where folders are located
$path = "C:\Users\mschi\Desktop\youtube\empty_folders"
# 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
}
# Check if the folder is empty
$folderContents = Get-ChildItem -Path $folderPath
if ($folderContents.Count -eq 0) {
# If folder is empty, create a zip file containing the empty folder itself
Add-Type -AssemblyName 'System.IO.Compression.FileSystem'
[System.IO.Compression.ZipFile]::CreateFromDirectory($folderPath, $zipFile)
Write-Host "Zipped (empty): $folderPath -> $zipFile"
} else {
# If the folder is not empty, zip its contents
Compress-Archive -Path "$folderPath\*" -DestinationPath $zipFile -Force
Write-Host "Zipped: $folderPath -> $zipFile"
}
}
Write-Host ""
Write-Host "Zipping process completed."