Anyone good with batch scripts? Trying to auto zip 3rd level folders individually

  • Thread starter Samus
  • 8 comments
  • 516 views
24,669
United Kingdom
United Kingdom
This is a simplified example of the folder structure I have:

Top-Folder
-- Bob
---- Folder-1
-- Steve
---- Folder-1
---- Folder-2
-- Tim
---- Folder-1

With about 700 second level folders like those named ones. If I used this batch script in the top level folder:

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

I'd end up with one zip for each named folder, bob.zip, steve.zip, tim.zip etc. Where my problem lies is that as per the Steve example some folders contain more than one sub-folder, and I need those to be separate zips. So the desired outcome would be:

bob.zip (Contains the folder-1 inside the bob folder)
steve1.zip (Contains the folder-1 inside the steve folder)
steve2.zip (Contains the folder-2 inside the steve folder)
tim.zip (Contains the folder-1 inside the tim folder)

Anyone know if this would be possible with a batch script placed in the top level folder? Or any other method? I've not found anything on google, they're all just the basic idea for first level zipping like above. Obviously I didn't think about the need to zip these all up when I created the folder structure but doing this all manually is going to be a PITA!
 
@Samus My scripting experience is with Unix shells so I can't really help with the specifics here, but I think you're going to just need to add a bit more control logic. Probably a nested FOR loop, so the outer loop iterates through Bob, Steve, Tim level as it currently does, but the inner loop will iterate through actually zipping up the folders contained within each (and you'll have to figure out how to derive the desired zip names based on where you are in the loops). Shouldn't be too hard to figure out.
 
I'm in the same boat with @David here, I'm familiar with Unix shell scripting and it would be straightforward to do what you want with bash (most common Unix shell). Which may sound like that's not terribly helpful on Windows.

However.

One possibility is installing the Cygwin package, which gives you a unix-like environment running natively on Windows including specifically bash.

Another intriguing option, which I confess I know nothing about other than what's in the link, is here.
 
So basically inside the "Bob" folder you'll get a zip of Folder 1, Folder 2 and so on. That happens inside each "Name" folder?

If so, I'll knock something up tomorrow... the easier way might be to write the script in VS then let that automatically issue the shell commands one by one.

EDIT: You could also do it from Word, Excel et al using the method I mentioned above. Any MS module writer that has access to the FileSystem object and which is able to Application.Run will do it :)
 
So basically inside the "Bob" folder you'll get a zip of Folder 1, Folder 2 and so on. That happens inside each "Name" folder?

If so, I'll knock something up tomorrow... the easier way might be to write the script in VS then let that automatically issue the shell commands one by one.

EDIT: You could also do it from Word, Excel et al using the method I mentioned above. Any MS module writer that has access to the FileSystem object and which is able to Application.Run will do it :)

Well yeah essentially, I'm not too concerned with where they are placed once created, I can easily pull them all out, it's just imperative that each folder inside the named folder is zipped individually, not as one big zip of them all as currently happens. Thanks.

Not too concerned how it's done, just that it works! :)
 
Well yeah essentially, I'm not too concerned with where they are placed once created, I can easily pull them all out, it's just imperative that each folder inside the named folder is zipped individually, not as one big zip of them all as currently happens. Thanks.

Not too concerned how it's done, just that it works! :)

Give me a few days, if that's okay? I was over-optimistic about how much free time Mrs. Ten is allowing on a sunny weekend :D

I'll knock it up in VS and supply the executable and source (in case you want to compile it yourself).
 
Give me a few days, if that's okay? I was over-optimistic about how much free time Mrs. Ten is allowing on a sunny weekend :D

I'll knock it up in VS and supply the executable and source (in case you want to compile it yourself).

Sure, I'm in no immediate hurry. Thanks.
 
Back