Zip all files in many sub-directories


Recommended Posts

Hi...

 

I have many folders and inside them I have many files, I wanted to zip all the files in each folder into one zip separately.

 

What I'm doing now is going to each folder, select all the files in there and zip them, but since I have thousands of folders, this will take forever.

 

Is there any way to do this automatically?

Link to comment
Share on other sites

It seems that this is possible with Powershell.

 

You can use the Add-Zip function from this site to write a script where you specify the directory that hosts your folders, and the target directory where you want the zipped files.

http://stackoverflow.com/questions/11021879/creating-a-zipped-compressed-folder-in-windows-using-powershell-or-the-command-l

function Add-Zip {
param([string]$zipfilename)
if(-not (test-path($zipfilename))) {
set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
(dir $zipfilename).IsReadOnly = $false }
$shellApplication = new-object -com shell.application
$zipPackage = $shellApplication.NameSpace($zipfilename)
foreach($file in $input) { 
$zipPackage.CopyHere($file.FullName)
do {
Start-sleep -milliseconds 250 }
while ($zipPackage.Items().count -eq 0) }}
$hostDirectory = "C:\src\tmp"
$targetDirectory = "C:\src\tmp"
$dirList = gci -Directory -Name $hostDirectory
foreach ( $dir in $dirList ) {
dir $hostDirectory\$dir *.* -Recurse | Add-Zip $targetDirectory\$dir.zip }

The disclaimer is that I don't actually know what I'm doing, but this seemed to produce the desired effect and didn't explode when I ran it.

post-17075-0-93191100-1402211030.png

Link to comment
Share on other sites

"The disclaimer is that I don't actually know what I'm doing, but this seemed to produce the desired effect and didn't explode when I ran it."

 

:D  :laugh:  :rofl:  :D  :laugh:  :rofl:

 

 

 

Gonna try it, thanks!

  • Like 2
Link to comment
Share on other sites

curiosity has gotten the better of me - why would you want/need to do this?

Link to comment
Share on other sites

It seems that this is possible with Powershell.

You can use the Add-Zip function from this site to write a script where you specify the directory that hosts your folders, and the target directory where you want the zipped files.

http://stackoverflow.com/questions/11021879/creating-a-zipped-compressed-folder-in-windows-using-powershell-or-the-command-l

function Add-Zip {
param([string]$zipfilename)
if(-not (test-path($zipfilename))) {
set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
(dir $zipfilename).IsReadOnly = $false }
$shellApplication = new-object -com shell.application
$zipPackage = $shellApplication.NameSpace($zipfilename)
foreach($file in $input) { 
$zipPackage.CopyHere($file.FullName)
do {
Start-sleep -milliseconds 250 }
while ($zipPackage.Items().count -eq 0) }}
$hostDirectory = "C:\src\tmp"
$targetDirectory = "C:\src\tmp"
$dirList = gci -Directory -Name $hostDirectory
foreach ( $dir in $dirList ) {
dir $hostDirectory\$dir *.* -Recurse | Add-Zip $targetDirectory\$dir.zip }
The disclaimer is that I don't actually know what I'm doing, but this seemed to produce the desired effect and didn't explode when I ran it.

What's the "ide" your using. I've never seen that for powershell. I might actually start looking into using command line with windows again. I hate command prompt and I love bash. Powershell seems to be the proper "command prompt" in windows.

Link to comment
Share on other sites

 

What's the "ide" your using. I've never seen that for powershell. I might actually start looking into using command line with windows again. I hate command prompt and I love bash. Powershell seems to be the proper "command prompt" in windows.

 

That is the Powershell IDE

 

Run cmd.exe as administrator and type the below... it will open the powershell ide

powershell ise
Link to comment
Share on other sites

 

 
 

That is the Powershell IDE

 

Run cmd.exe as administrator and type the below... it will open the powershell ide

powershell ise

 

Thank you. Going to try it out tonight.

Link to comment
Share on other sites

I did run the script and it gave me an error

 

 

Get-ChildItem : A parameter cannot be found that matches parameter name 'Directory'.

At line:15 char:26

+ $dirList = gci -Directory <<<<  -Name $hostDirectory

    + CategoryInfo          : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException

    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

 
 
I also found a possible solution in WinZip, "Put each file to separate archive". The perfect solution was to zip just what's inside the folders, this option zips the files inside zipping the folder itself (and it's contents, of course). 
Link to comment
Share on other sites

 

I did run the script and it gave me an error

 

 

Get-ChildItem : A parameter cannot be found that matches parameter name 'Directory'.

At line:15 char:26

+ $dirList = gci -Directory <<<<  -Name $hostDirectory

    + CategoryInfo          : InvalidArgument: ( :) [Get-ChildItem], ParameterBindingException

    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

 

Glad you got some acceptable solution worked out.

 

For the sake of not leaving a broken script broken, it looks like the -Directory switch doesn't exist in previous versions of Powershell. Fortunately, you can tickle them into spitting out a list of directory names anyway; the line

$dirList = gci -Directory -Name $hostDirectory

would be replaced with:

$dirList = gci $hostDirectory | ?{$_.mode -match "d"} | Select-Object Name

At least, that seems to work on Windows 7.

Link to comment
Share on other sites

For the sake of not leaving a broken script broken, it looks like the -Directory switch doesn't exist in previous versions of Powershell.

 

Better still, update PowerShell to v3.0 or v4.0. Using the -Directory switch will be faster.

 

v3.0: http://www.microsoft.com/en-gb/download/details.aspx?id=34595

v4.0: http://www.microsoft.com/en-gb/download/details.aspx?id=40855

Link to comment
Share on other sites

wouldn't something like this work?

 

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

 

So looking around for a test dir on my system, seems I had some dell drives in a folder, etc..

 

post-14624-0-03063200-1402319171.png

 

So pasted the simple for loop in the dir where all the folders are - and bing bang zoom zips of all the folders.

 

You would use %% if you put in a batch file, etc.

Link to comment
Share on other sites

Sorry, had a lot of work lately.

 

I tried 

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

 

But it's the same has doing WinZip (Put each file to separate archive). It compress the folder and not just the files inside of it.

 

I think like this works but the perfect solution was to zip just the files inside.

 

Thanks everyone for the help!

Link to comment
Share on other sites

WinZip does it "automatically", just select the folders then the option "Put each File to separate archive". But, like I said, it compress the files inside the folder and the folder itself.

 

 

Don't know what happened, someone asked if this was possible to do automatically and I did answer, but the post is gone :wacko:

Link to comment
Share on other sites

"I think like this works but the perfect solution was to zip just the files inside."

 

What??  Zip the files inside to what, individual zips???  How do you zip stuff inside a folder, but not the folder??

Link to comment
Share on other sites

If you zip folder A1, when you unzip it, it shows the Folder A1 and it's contents. If you zip all the files inside Folder A1, then you unzip it, it'll show just the files that were inside Folder A1.

 

Example: Comic Books

 

Scan the comic book and put the .jpg inside a folder (B2), then zip B2. Most of the comic readers will read all the files inside B2, but others don't. If I select all the .jpg inside B2 and zip them, then all the Comic Readers will read the file.

 

Did I explain myself correctly?

Link to comment
Share on other sites

so you don't want the path in the zip then..  That is easy enough to do if your in the folder the path will not be included with 7z - you would have to cd to each folder than run the command..  Makes the command a bit more complicated..  I would just use winrar if you need to get fancy..  Now rar can not create zip, but winrar can.  And you can call it from cmd line..  You will just get a gui popup showing progress when you run it.

 

So you want this

 

post-14624-0-31853800-1402835795.png

 

Vs with the 7z command I gave produces this for the same folder

 

post-14624-0-64398000-1402835845.png

 

Now if the folders have subfolders and you just don't want the base path this command works

 

for /d %X in (*) do "C:\Program Files\WinRAR\winrar.exe" a -ep1 -r "%X.zip" "%X\"

 

This will create zip with no base - so files in the root of the archive, but with subfolders listed..  If you don't have subfolders are don't want to zip them.

 

for /d %X in (*) do "C:\Program Files\WinRAR\winrar.exe" a -ep "%X.zip" "%X\"

 

But you will only get files that are in root of the folders..  I think the first one is the one your most after..  It can be done with 7zip as well - but you have to do a cd to each folder in your command.  Might be able to do something with pushd and popd to maintain where your at in the folder structure for zipping - but use of winrar is just easier because they have a switch to turn of path(s).

 

So for example the archive will not have a base folder, but any subfolders will be included..  Like this

 

post-14624-0-62299400-1402836014.png

Link to comment
Share on other sites

This topic is now closed to further replies.