The tool that I used for a long time for extracting and compressing archives is 7-Zip. It’s free, open-source, and has a powerful set of command line options that is perfect for performing backups. And today that’s exactly what we’re going to use it for.
We’re going to use a special version of 7-Zip that has no user interface whatsoever. It has to be used via the command line, and one of the benefits for using this version is that the entire application is self-contained in one single executable file. That makes it a great tool to carry around with you on a USB drive, and extremely easy to work with.
Here’s how to use the 7-Zip command line options to create backups at the click of a mouse:
- The first thing you’ll want to do is go download the command line version of 7-zip. After you extract the contents you’ll notice that there are five files available. All we need is the 7za.exe, so you can delete the rest if you wish.
- Open up Notepad, and now we’re going to create a batch file that will backup a folder location of our choosing. I’ve decided to backup everything on my desktop this time around, but it will work for any location as long as you know the path.
7za.exe a cybernet.7z "C:\Users\Ryan\Desktop"Be sure to include quotation marks around any paths that have spaces in them. For this example I didn’t have to put quotation marks around my path, but it never hurts to include them.
This will archive the documents in the 7-Zip format, which means you’ll need to have 7-Zip to extract it. It’s good because the compression ratio is very high, which means the backup file will be rather small compared to the other compression formats. If you were going to send this to someone they may have troubles opening it, but you can choose save the file as a ZIP by replacing “7z” with “zip” in that line of code.
- Now we need to save the batch file in the same place where the 7za.exe is located. In Notepad go to File -> Save As, and choose any filename. The only thing you need to make sure of is that at the end of the filename you include the .BAT extension:
- Now all you have left to do is double-click on the batch file that you just created to run it. The resulting backup file will be located in the same place as the 7za.exe and the batch file, unless you’ve specified an alternate location in Step 2.
- Extra Credit: You can always create a scheduled task that will run your batch file automatically at a time/date that you specify. That can be done by using the built-in Windows Scheduled Tasks wizard, which can be found in the Control Panel.
One other tip that I wanted to provide is how to automatically insert the current year, month, and day into the resulting filename. This would be useful for anyone looking to schedule backups, without wanting to overwrite the previous backup. In Step 2 from our example above you would want to replace:
cybernet.7z
with:
%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_cybernet.7z
which for today would give a result of:
2008-01-03_cybernet.7z
Now every backup will remain in tact, assuming that you create no more than one backup per day.
For those of you who want to dive a little deeper in to the 7-Zip command line options, here are the different things that the “a” can be replaced with in Step 2:
- a: Add to an existing archive, or create a new one
- d: Delete files from an archive
- e: Extract files from an archive
- t: Test the integrity of an archive
- u: Update the files in an archive


I definitely agree – 7zip is a wonderful tool, flexible and very powerful. Thanks for the great tip, by the way! I’m recently rediscovering the utility of the command line.
Additionally, if you’re interested in a portable version that may be a little more inline with everyday use (e.g., non-command line), try 7-zip Portable, brought to you by the people over at PortableApps.com: [portableapps.com]
I keep that 7-Zip portable on my Flash drive all the time. It’s a great tool to have.
Very useful, Ryan! Stumbled and del.icio.us’ed! I have been looking to automate my document backups in my laptop. Combine this with FileHamster, I can automatically move the backed up zip files to my pen drive anytime I plug that in!
TERRIFIC IDEA (Ryan and K-IntheHouse)!!
Hadn’t heard of FileHamster before, but it looks like a real gem. We’ll have to write about that.
You should take a look at Cobian Backup [educ.umu.se]~cobian/cobianbackup.htm version 9 Beta named Amanita. It now includes 7-zip compression and a great GUI for automating all your backup tasks. I have been using Cobian for years and it really is one of the best free programs I know of.
I’ll definitely have to check that app out because it looks pretty sweet.
There is a very thorough description of the command-line syntax here:
[prudentialscatterbrain.hp.infoseek.co.jp]
Very useful if you want to use other compression methods.
I need help with the following and I appreciate if you can help me out.
Let say you have one file (*.txt) on your desktop which gets updated every month using a different name. In this case I want 7zip to create an archive every month using whatever name the file has. Can this be done?
7za.exe a *.7z “C:\Users\Ryan\Desktop\*.txt”
Yes that concept works. You can use an asterisk in place of a filename in order to specify a certain file extension. However, please be aware there are a couple of catches (see [7-zip.org]):
1) You cannot use the *.* form of a wildcard. You have to simply use a single *
2) If you chose to use certain specific archive compressions (i.e., .7z), you’ll have to use the “-ms=off” switch (disable solid mode) in order to be able to later add files to that archive.
You also may want to use the “u” command in place of the “a” command. This will guarantee that the newest version of any existing files will be updated.
In short, it should look something like this:
7za.exe u -tzip “c:\location_of_archive\archive.zip” “c:\location_of_files\*.txt”
I strongly recommend you consult the help file (7-zip.chm – seen in the screencap in the article) included in the [sourceforge.net] for all the details about command line usage.
Thanks for helping Lappy!
No problem! I spent quite a bit of time dealing with the 7-zip command line, for the purposes of making my own batch files in order to automate backups.
Learning and effectively using 7za.exe is terrific!
Excuse my ignorance, but what would the structure be to 7za a folder with the previous day’s date on it?
I can help with making a file timestamped for the current date, but I don’t think I know how to make one timestamped for the previous day.
It is actually rather simple – all you have to do is insert the following prompts into the filename (within the batch file):
Current hour: %TIME:~0,2%
Current minute: %TIME:~3,2%
Current second: %TIME:~6,2%
Current day of week: %DATE:~0,3%
Current month: %DATE:~4,2%
Current day date: %DATE:~7,2%
Current year (2 digit): %DATE:~-2%
Current year (4 digit): %DATE:~-4%
Current DOW MM/DD/YYY %date%
Current HH:MM:SS.hh %time%
(see [simultaneouspancakes.com] [xona.com] and especially [intelliadmin.com])
An example of what a batch file for creating an archive with a timestamp of the “month – day of month – year” is as follows:
.\location_of_7zip\7za.exe u -tzip “c:\location_of_archive\%DATE:~4,2%-%DATE:~7,2%-%DATE:~-2%.zip” “c:\location_of_files\*.txt”
(make sure to have the quotation marks if you choose to have spaces in the filename)
If anyone can take this to the next step and figure out how to do a similar task, but the a previous date, I’m sure there would be many grateful readers!
I looked all over, and couldn’t find anything that would let you do the previous day. It’s gotta be possible though.
I know – that’s what I thought too!! Pretty please do let us know if you do actually find anything, anything at all! I would be very grateful indeed.
It can be done and various scripts exist on the web (one such script can be found at [robvanderwoude.com]). The problem is that it’s too complex for the triviality of the issue and usually it’s recommended to not use batch files, but WSH if you need this functionality.
Hey would you trap any errors reported by 7-zip when run from the command line?