Have powershell zip the contents of a bunch of folders, individual zip for each folder

Posted by WebDevHobo on Super User See other posts from Super User or by WebDevHobo
Published on 2010-01-29T17:33:11Z Indexed on 2010/04/24 4:03 UTC
Read the original article Hit count: 570

Filed under:
|

Recently, I asked how to do this with a .bat file and an answer was provided.

for /D %%d in (*.*) do "C:\Program Files\7-Zip\7z\7za.exe" a -tzip %%d.zip %%d

However, this proved useful only for folders that have no spaces in their name. The reason being that batch will do the following: if the folder name is "jef's vacation pics", the variables will be:

%%d = jef's
%%e = vacation
%%f = pics

And then it tries to pass only %%d to the 7-zip program, which will not find such a folder and therefor will not create a zip file.

I've tried looking up some tutorials, documentation sites and such, but I haven't been able to come up with an answer. There may be an answer, but I want to take this opportunity to try my hand at powershell.

I was thinking that a function with 1 argument, that being the parent-folder of the sub-folders that need to be zipped, would be the best approach.

So here's what I have, which doesn't work, probably due to my general in-experience with powershell:

function zipFolders($parent) {
  $zip = "C:\Program Files\7-Zip\7z\7za.exe";
  $parents | ForEach-Object $zip a -tzip
}

© Super User or respective owner

Related posts about powershell

Related posts about 7zip