Wednesday, 27 April 2016

7-Zip 7za command line zip tool

7-zip is an alternative to the native Windows zip function, WinZip or similar program for compressing files. 7-zip is free of charge, reliable and supports many formats. I wrote an article about it back in 2010, here's the link: https://mgxp.blogspot.ch/2010/06/review-7-zip.html

7-zip is primarily a Windows application, with a graphical user interface. Normally that's fine and often it's enough to manage your compressed files. However, sometimes it would be nice to automate the compressing of files. Maybe you want to backup files by just double clicking a file? In this case you can use 7za.exe, a command line program.

This article is an introduction to the 7za.exe command line tool. We'll have a look at how to download it and as an example we'll see how we can use it to backup files to a removable drive.


Download
Browse to http://www.7-zip.org/
Download and install the 7-zip Windows program.

On the same web page, on the Downloads page look for '7z Library, SFXs for installers, Plugin for FAR Manager' - click to download the 7zXXX_extra.7z file.

7z is the native compressed format of 7-zip, you can open it using the 7-zip Windows program you just installed. Extract the files to a folder. In the folder you'll find a file called 7za.exe. You don't need all the other files, you can just copy this one 7za.exe file and use this for your command line projects.


Example: Simple Backup
In this example I have a local D: drive with a folder called 'source' where I have some important files I want to backup. I have a USB flash drive that is drive E.
  1. Insert a USB flash drive
  2. Copy the 7za.exe file to the USB flash drive (for example)
  3. Open a command window (press Win-R, type CMD and press Enter)
  4. At the command prompt change to the USB drive where you put the 7za.exe file (in our example I will type E: [Enter] because my USB drive is drive E)
7za u -r "backup" "d:\source\*.*"       [Enter]


Example: backing up files from the local D: drive to a file called "backup" on the E: (USB drive)

The above command will take all the files in D:\source, compress and copy those files to a called backup.7z on my USB flash drive (drive G: in my case). Later if I update files in D:\source I can use the same command to update the backup.7z file. 

Let's have a closer look at the commnad line parameters we used: 

This means to update, only those files that have changed will be added/updated in the 7z file. 

-r
This means to be recursive, it will copy all files from the source folder and sub-folders. 

"backup" 
In our example this will create a compressed archive file. All the files from the source folder will go inside this one compressed file. By default 7za will use its native 7za format, which is recommended. 

"d:\source\*.*" 
The files to backup, combined with the -r parameter it means that any folders/files below this folder will also be included. 


Command File (batch file) 
To make your life easier you can put the command line into a file and save it with the 7za.exe file on your USB drive. Whenever you need to backup those files just double click the command file. 

Open Notepad and enter the following two lines: 
7za u -r "backup" "d:\source\*.*" 
pause 

Save the file as 7zaBackup.cmd 


At any time you can double click 7zaBackup.cmd to run your backup. 


Help 
At the command prompt you can type:
7za -h   [Enter]
...to get a list of parameters. More help is available at http://www.7-zip.org


Reference 
If you are looking for some help with creating self-extracting archives, please see my previous articles specifically on this more advanced subject: 

An alternative for backing up files using the command line is RoboCopy. Click the following link to find many articles about this powerful tool: http://mgxp.blogspot.com/search/label/Robocopy

Post a Comment