CyberNotes
Tutorial Thursday

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:

  1. 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.
    7zip Command Line Executable
  2. 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.

    7zip Command Line Notepad

  3. 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:
    7zip Command Line Notepad Save
  4. 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.
  5. 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

There Are 29 Comments

  1. 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]

  2. I keep that 7-Zip portable on my Flash drive all the time. It’s a great tool to have.

  3. 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! :P

  4. K-IntheHouse wrote:
    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! :P

    TERRIFIC IDEA (Ryan and K-IntheHouse)!!

    • It seems great, but can we delete the files after zipping them to different folder? I didn’t see any command in 7-zip to do this…appreciate if anybody help on this…

    • You can use a batch file to delete the files. Just use the “del” command.

    • Trying to zip log files but couldn’t…i am having files like abc.log.0, abc.log.1,abc.log.2…i need to zip files starting from 1…Any idea, how to go about it?

  5. K-IntheHouse wrote:
    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! :P

    Hadn’t heard of FileHamster before, but it looks like a real gem. We’ll have to write about that. :)

  6. 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.

  7. I’ll definitely have to check that app out because it looks pretty sweet.

  8. 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.

  9. 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”

  10. Sam wrote:
    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.

  11. Thanks for helping Lappy!

  12. Ryan wrote:
    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!

  13. Excuse my ignorance, but what would the structure be to 7za a folder with the previous day’s date on it?

  14. richlich wrote:
    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!

  15. I looked all over, and couldn’t find anything that would let you do the previous day. It’s gotta be possible though.

  16. Ryan wrote:
    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.

  17. Lappy wrote:
    Ryan wrote:
    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.

  18. Hey would you trap any errors reported by 7-zip when run from the command line?

  19. Can you specify an output directory using a UNC path?

  20. I would archive the files depending on the creation date. Is there a possibility? I didn’t find something about this in the help or in the community. Thanks in advance!

  21. I need to compress bulk subfolders separately how would i do that, can someone help with the commandline

    btw you guys make it look so easy… its not

  22. I have a problem when using the date stamp command as indicated above.
    Using Ryan’s original example in the main article where hew says:

    %DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_cybernet.7z

    which for today would give a result of:

    2008-01-03_cybernet.7z

    I get the following output: (Considering the date is 2010-08-05)
    7Zip creates a the following with the date elements making up separate folders…
    08/05-/0-/0_cybernet.7z

    I am using the date stamp code exactly as in the example.

    What am I doing wrong?!?

    • I also had problems with the output not correctly formed, but I’ve solved modifying the numbers in the string:

      my system when I type DATE command shows the date in the format DD/MM/YYYY

      so, i’ve modified the string with the format %DATE:~-4%-%DATE:~3,2%-%DATE:~0,2%

      try to look what format is using your system {type DATE without parameters) and then adjust the numbers in the string {they are the positions of the char to show)…

      Bye

  23. ARIF ABDUL GAFFAR DADANI

    Another way to get the date format with – as separator is to open the regional settings in the control panel & select customize. Then go to the tab named date. Here, change the date separator from “/” to “-” and OK.

    You can check the format by going to the cmd prompt. Type echo %date% and press enter

    example
    c:\>echo %date%
    c:\>2010-09-22

  24. Has anyone any idea for creating log files?
    The script works perfect.
    Thank you

Leave Your Comment


Message is the only required field.
Emails are not published.