I'm trying to compress each folder in my directory into a zip with the same name
I tried the simple compress-archive command
Compress-Archive $targetFolderPath -Destination $zipFolder -Update
but it gives me only one zip named ".zip" with all my folder in it, that's not what I want
Then I've found this, which is my best attempt :
Get-ChildItem $targetFolderPath -Directory | foreach { compress-archive $_ $_.basename -update}
I find one folder for one zip with the correct name which is good, however I cannot choose the destination directory
Can anybody see what I need to do to make this run ?
Thank you
CodePudding user response:
Here's what I came up with. You can still transform it to a one-liner, if so desired, but I prefer a little more verbosity as the code is easier to read.
$Folders = @(Get-ChildItem $TargetFolderPath -Directory)
foreach ($Folder in $Folders) {
$Zipfile = Join-Path -Path $ZipFolder -ChildPath ($Folder.name '.zip')
try {
Compress-Archive `
-Path $Folder.FullName `
-DestinationPath $Zipfile `
-CompressionLevel Fastest `
-Update `
-ErrorAction Stop
} catch {
Write-Warning "Unable to archive $($Folder.FullName): $($_.Exception.Message)"
}
}