Archiving Windows Directories With 7-Zip and a FOR Loop

Posted on in System Administration

I recently needed to archive a bunch of old home directories on a CIFS share mounted on my Windows XP virtual machine. I wanted each home directory to be archived independently so that I would have lots of smaller zip files rather than one huge one. Additionally, maintaining the directory name in the zip file name would make it easier to a specific user's data in the future.

<!--more-->

Windows XP does not have a built-in command line zip application. For this, I downloaded 7-Zip. 7-Zip is open source software and supports a bunch of compression algorithms. More importantly, it comes with a command line executable! Once you've installed the latest version of 7-Zip, you should add it's location to your Path environment variable so that you can execute the 7z command. As you can see in the image below, I've added C:\Program Files\7-Zip to my Path variable.

7-Zip in Windows Path Environment Variable

Once this was done, I archived all the directories in my Y: drive with the following FOR loop:

for /D %DIR in (*) do 7z -a Z:\%DIR.zip %DIR

This command was run from the Y: drive root directory and the zip files were stored in the root directory of the Z: drive.

My Bookshelf

Reading Now

Other Stuff