Sunday, April 20, 2014

Batch Compressing (and decompressing) Multiple Directories

Hi all, thought I'd share another cool thing I did.

I like to back up my old data, and I have a ton of directories with hundreds of thousands of files in them. Compressing them would make them 1. much smaller, and 2. easier to store. But compressing everything into one file would be dangerous and very difficult to manage, and compressing every folder individually would take forever.

Fortunately, 7-zip has some nice command line tools. Using these, I wrote a small script that I can put anywhere, and double-clicking on it will compress every folder next to the script into a separate .zip file, AND test them for errors. Here's how:

Compressing

Step 1: Get 7-zip (Google it).

Step 2: Make a new text file, and paste in the following code (in blue):

@echo off
for /d %%G in (*) do "C:\Program Files (x86)\7-Zip\7z.exe" a "%%G.zip" "%%G\" -mmt
for %%G in (*.zip) do "C:\Program Files (x86)\7-Zip\7z.exe" t "%%G" * -r  | FIND /v "Testing " | FIND /v "Copyright "
pause

NOTE: You may need to change the "C:\Program Files (x86)\7-Zip\7z.exe" sections to point to where you installed 7-zip, if it's different from me.

Step 3: Save this file with any name, with the .bat extension (for example, batch-compressor.bat).

Step 4: Put this file somewhere that has a bunch of directories in it.

Step 5: Double-click the file (do NOT run as administrator). A command prompt will pop up and show you the progress. When it is done, look through and make sure there were no errors and all the sections say "Everything is OK".

You should notice a bunch of .zip files next to your folders now! Just delete the folders if you don't want them anymore, and keep the .zip files.

Notes:
  • This only compresses folders and the files in those folders, not files in the same directory. You'll need to do that yourself if you want.
  • This does not delete the original folders; if you want to do that, just delete them when it's done. Make sure your drive has enough room for all the new .zip files, especially if you're compressing a lot of data.
  • Personally, I would recommend not making each zip file larger then a few GB each, preferably much smaller. Too big archives are at higher risk of corruption or some issue happening, which would affect a larger amount of data, and it's also a pain if you want to move specific files around later.
Decompressing

Now how do you decompress (extract) these archives you just made? Again, decompressing them manually would take forever. Well, there's a script for that too.

Step 1: Make a new batch file, the same way as above. Paste in the following code (in blue):

@echo off
for %%G in (*.zip) do "C:\Program Files (x86)\7-Zip\7z.exe" x "%%G" -mmt
pause

NOTE: Again, you may need to change the "C:\Program Files (x86)\7-Zip\7z.exe" section to point to where you installed 7-zip, if it's different from me.

Step 2: Save the file with a new name, and as a .bat (for example, batch-decompressor.bat)

Step 3: Put the file next to all the archives you want to extract.

Step 4: Double-click the file (do NOT run as administrator). A command prompt will pop up and show you the progress.

And you're done! Now you have a way to compress lots of folders into individual archives, and a way to extract all of them again in one fell swoop.

Happy archiving!

2 comments:

  1. Or if you're using Linux, you could just do the following:
    for dir in */; do tar -zcvf $dir.tar.gz $dir; rm -rf $dir; done

    And to unzip, do:
    for i in *.tar.gz; do tar -xzf $i; done

    This works out of the box on any Linux distro, no additional software needed :)

    ReplyDelete
    Replies
    1. I'd love to program in Shell, it's so much better than batch, but I guess I have to make do :/

      Delete