Jump to content



Photo

Zip all files in many sub-directories

win7

  • Please log in to reply
19 replies to this topic

#1 Pedro3

Pedro3

    Neowinian

  • Joined: 02-August 04

Posted 08 June 2014 - 03:20

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?




#2 zhangm

zhangm

    Just bitter.

  • Tech Issues Solved: 23
  • Joined: 21-August 02

Posted 08 June 2014 - 07:03

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

Attached Images

  • psscript.png


#3 OP Pedro3

Pedro3

    Neowinian

  • Joined: 02-August 04

Posted 08 June 2014 - 07:28

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



#4 +BudMan

BudMan

    Neowinian Senior

  • Tech Issues Solved: 106
  • Joined: 04-July 02
  • Location: Schaumburg, IL
  • OS: Win7, Vista, 2k3, 2k8, XP, Linux, FreeBSD, OSX, etc. etc.

Posted 08 June 2014 - 13:05

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



#5 Sikh

Sikh

    Neowin Addict!

  • Tech Issues Solved: 3
  • Joined: 11-March 07
  • Location: localhost
  • OS: Windows 7 / 10.8 / Ubuntu Server
  • Phone: Nexus 5 PA 4.4.2 / iPhone 5

Posted 08 June 2014 - 15:35

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

#6 brink668

brink668

    Neowinian

  • Joined: 12-September 07

Posted 08 June 2014 - 15:56

 

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


#7 Sikh

Sikh

    Neowin Addict!

  • Tech Issues Solved: 3
  • Joined: 11-March 07
  • Location: localhost
  • OS: Windows 7 / 10.8 / Ubuntu Server
  • Phone: Nexus 5 PA 4.4.2 / iPhone 5

Posted 08 June 2014 - 16:01

 

 
 

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.



#8 elenarie

elenarie

    Newbie .NET / game dev

  • Tech Issues Solved: 2
  • Joined: 23-March 14
  • OS: Windows 8.1 Pro x64
  • Phone: Lumia 920 Yellow

Posted 09 June 2014 - 03:54

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



#9 OP Pedro3

Pedro3

    Neowinian

  • Joined: 02-August 04

Posted 09 June 2014 - 04:40

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


#10 zhangm

zhangm

    Just bitter.

  • Tech Issues Solved: 23
  • Joined: 21-August 02

Posted 09 June 2014 - 05:26



 

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.



#11 Shiranui

Shiranui

    Iconoclast

  • Tech Issues Solved: 3
  • Joined: 24-December 03

Posted 09 June 2014 - 05:41

http://www.wikihow.c...lders-in-One-Go



#12 Ace

Ace

    Neowinian

  • Joined: 06-November 01
  • Location: United Kingdom

Posted 09 June 2014 - 05:55

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...s.aspx?id=34595

v4.0: http://www.microsoft...s.aspx?id=40855



#13 OP Pedro3

Pedro3

    Neowinian

  • Joined: 02-August 04

Posted 09 June 2014 - 06:16

 

But isn't this one the same as "Put each file to separate archive" in WinZip?



#14 Shiranui

Shiranui

    Iconoclast

  • Tech Issues Solved: 3
  • Joined: 24-December 03

Posted 09 June 2014 - 06:40

Haven't tried it.



#15 +BudMan

BudMan

    Neowinian Senior

  • Tech Issues Solved: 106
  • Joined: 04-July 02
  • Location: Schaumburg, IL
  • OS: Win7, Vista, 2k3, 2k8, XP, Linux, FreeBSD, OSX, etc. etc.

Posted 09 June 2014 - 13:06

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

 

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.