Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have folders full of photos within category folders

eg

  • holidays
    • spain
    • france
    • italy
  • family
    • son
    • daugher
  • christmas
    • 2010
    • 2011

... except much longer ... there are about 100 folders.

I need to compress each sub folder ( ie spain, france italy, son, daughter etc) individually; so that, for the holidays category folder for example I would end up with:

  • holidays
    • spain
    • spain.zip
    • france
    • france.zip
    • italy
    • italy.zip

... and so on. ( or using rar files )

I want to avoid doing this manually if possible, and I think it's possible from the command line using WinRar, (though I could use something else if that was better). Could someone give me (and briefly explain to me) the code I'd use, and how to use it. I know a little about the command line but I have fairly limited experience.

I am using Windows 7

share|improve this question
    
If you don't even know how to begin to write a program like this then you don't have the skills to do it. A question like this isn't a good fit for Superuser. –  Ramhound May 21 '12 at 15:33
4  
Since his request can be completed with one DOS command, it seems like an exact fit for Superuser. –  Tom May 21 '12 at 15:40
add comment

5 Answers

I don't think this will be easy using a batch file, but it should be possible.

Batch allows you to iterate through directories using a for loop like this:

for /d %%X in (directorySet) do ( command )

/d is required to specify that you will be looping through directories
%%X is a directory in the list
command is what to run like 'echo "hello"'

Source: http://www.robvanderwoude.com/for.php

This is all I really know that might help you, what you use in place of command is very dependant on the program you are using.

It would be easier in Powershell as this has more bash-like notation, and is much more flexible, but using batch is completely fine.

Sorry that's all the help I could give, I have very minimal knowledge of batch...

share|improve this answer
add comment

I don't have a Windows system handy to cook up your command,
But, here is a rough list of things,

  • you would use the dir command in a for loop (or, maybe you need a more recent manual page)
  • limit your dir listing to depth=1 directories
    • /A:D Folder switch (?)
    • some way to not-recurse or use depth==1 (I've forgotten windows batch files now)
  • Check 7z too
  • If you are often into windows scripting check PowerShell
share|improve this answer
    
forfiles is another option –  Sathya May 21 '12 at 15:57
add comment

In DOS, change to the parent folder that has your 100+ folders, then run one "for" command, like this:

for %v in (*.*) do pkzip %v.zip %v

Replace pkzip with your command-line zip/archive tool. The 2nd and 3rd "%v" expand to the folder names.

share|improve this answer
    
Ah, yes -- DOS does not recurse into folders by default. Good. –  nik May 21 '12 at 15:52
add comment

Check out my answer to the similar SU question titled "How do I create seperate 7z files from each selected directory with 7zip command line?". It could easily be adapted to use a different command-line zip/archive tool, just change the part after the DO in the statement.

I personally prefer 7-Zip over WinRAR and the couple of other compression utilities I tried. It's powerful, quick, and free. Note that it's unlikely that your image files will be able to be compressed much since by the nature of their format they're usually already compressed with sophisticated image-oriented algorithms which a generally better than any generic one used in a compression utility -- although the non-free WinZip program claims can further reduce JPEG image files by 20-25%.

share|improve this answer
    
thanks, yes I know, I only want to zip them so that I can create a single file out of several. –  byronyasgur May 22 '12 at 0:02
1  
@byronyasgur: In that case, most utilities have an option to not attempt to compress the files being added which would speed up the process. For 7-Zip see the -m switch in the help file (7-zip.chm in the command-line version download). BTW, 7-Zip can also create .zip files if you prefer them for some reason). –  martineau May 22 '12 at 16:50
    
good tip thanks –  byronyasgur May 22 '12 at 20:17
add comment
up vote 0 down vote accepted

I thought it would be better to answer my own question and accept that, despite the helpful answers, (giving upvotes to everyone instead of picking an answer) ... so that future visitors would get a concise answer. This is because I ended up getting my result from a combination of the four answers so here was tested solution I ended up with.

I changed to 7zip ( downloading the command line version and placing it in my user folder) because it seems to have better documentation on command line operation than Winrar.

Then I created a batch file, which I ran from the folder whose subfolders I wished to zip; and put into it the following command which I derived from the other answers:

for /d %%i in (*.*) do "c:\users\user\7za" a -tzip "c:\path\to\output\folder\%%i.zip" "%%i"

(My username is 'user' so this would be adjusted according to an individual's Windows 7 username)

I tested the command removing various aspects eg the /d and the -tzip and I didn't find it to work unless it was all in place.

I also found this to be a good resource http://www.dotnetperls.com/7-zip-examples

NB I was not concerned with folder depth and in my case all the sub folders are only one level below the top level folders. Also these folders only contain files, not more folders. I doubt if the above code would work for folders contained within folders but i'm sure it could be adapted easily.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.