Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to compress multiple folders and delete them after the compression. I have all of them inside a single folder and I have zipped multiple folders successfully. But I am not able to delete those folders after zipping them. Might be my scripting for the Remove-item is wrong. Please Help me.

What I have tried:

$path = "C:\Users\eps\Desktop\Archive"

$source = Get-ChildItem -Path C:\Users\eps\Desktop\Archive 

Add-Type -assembly "system.io.compression.filesystem"

Foreach ($s in $source)

 {

  $destination = Join-path -path $path -ChildPath "$($s.name).zip"

  If(Test-path $destination) {Remove-Item $path}

  [io.compression.zipfile]::CreateFromDirectory($s.fullname, $destination)}
Posted
Updated 13-Jul-20 23:08pm

1 solution

You are trying to remove $path, which is the Archive directory. You need to combine $path with $s and remove that.
 
Share this answer
 
Comments
Maciej Los 14-Jul-20 7:19am    
Excellent!
Richard MacCutchan 14-Jul-20 7:43am    
:thumbsup:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900