Creating and running a batch file for compressing directories


A batch file is a series of commands that can be executed together to accomplish a particular task. Batch files save time and effort when tasks have to be executed repetitively. A batch file contains unformatted text (ASCII), is best created using the Notepad editor, and needs to have a .bat file extension. To run the batch file, double-click on the icon.


Creating and running a batch file

Create a directory on your local hard disk where you want to store your compressed backups. E.g.:

c:\backups

Download the compression utility zip.exe the example batch file backup-daily.bat from here, and save and uncompress them in the backup directory.

Open the file backup-daily.bat in the notepad editor and apply the changes as discussed below:



The first 7 lines in the batch file are comments that describe the date format settings required for the batch file to work. If your short date format and the date separator are not set as shown below, the batch file may not work properly.



zip -ur backup-last.zip \\NETWIN\CESD\BBouman

The above command runs the compression utility zip.exe starting from the directory \BBouman in the \\NETWIN\CESD repository. All files and sub-directories in \BBouman are compressed together into the local file backup-last.zip. The directory \BBouman is only an example, and you need to replace it with the path to the directory from where you want the backup to start. You need read access to the directories that you want back up.

zip -ur backup-last.zip V:\BBouman

If you have mapped the network drive and repository to a particular drive letter as shown below, you can use the drive letter and directory name.



copy backup-last.zip backup-%date%.zip


The above command makes a copy of the file backup-last.zip and replaces the word last with the current date. After running the batch file for the first time, the directory should look similar to the screen shot below.



Every time you run the batch file, an additional backup file with the current date is created.

Discussion

options for zip utility

  • u updates the existing compressed file only with new or changed files. This option is used to reduce network traffic
  • r causes recursive traversal of the directory structure. It means that all files in the directories and subdirectories will be included in the compression.

When using the u option, files and folder that are deleted from the repository will still remain in the compressed backup. To avoid this accumulation, you can create a separate batch file backup-monthly.bat that makes a copy of all the recent files.

del backup-last.zip
zip -ur backup-last.zip \\NETWIN\CESD\BBouman
copy backup-last.zip backup-%date%.zip

The command del will delete the backup-last.zip file. Deleting will stop the file accumulation, thus creating a fresh version of the backup file. Depending on the number and size of files, this may cause considerable network traffic and should only be run occasionally, e.g. monthly.

Note that you will need to change it in all places for the batch file to work properly.


Last modified April 21, 2008 5:57 am