Jump to content

20 posts in this topic

Posted

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?

Share this post


Link to post
Share on other sites

Posted

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.

1 person likes this

Share this post


Link to post
Share on other sites

Posted

"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!

2 people like this

Share this post


Link to post
Share on other sites

Posted

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

Share this post


Link to post
Share on other sites

Posted

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.

Share this post


Link to post
Share on other sites

Posted

 

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
1 person likes this

Share this post


Link to post
Share on other sites

Posted

 

 
 

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.

Share this post


Link to post
Share on other sites

Posted

If you've PS pinned on the taskbar, you can start the ISE from there as well. Or just write "ise" in PS.

Share this post


Link to post
Share on other sites

Posted

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). 

Share this post


Link to post
Share on other sites

Posted



 

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.

Share this post


Link to post
Share on other sites

Posted

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

Share this post


Link to post
Share on other sites

Posted

Haven't tried it.

Share this post


Link to post
Share on other sites

Posted

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..

 

[attachment=362163:zipup.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.

Share this post


Link to post
Share on other sites

Posted

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!

Share this post


Link to post
Share on other sites

Posted

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:

Share this post


Link to post
Share on other sites

Posted

"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??

Share this post


Link to post
Share on other sites

Posted

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?

Share this post


Link to post
Share on other sites

Posted

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

 

[attachment=362301:nopath.png]

 

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

 

[attachment=362303:withpath.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

 

[attachment=362305:nobasebutsub.png]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.