Wednesday, July 5, 2006
By: Matthew Doucette
WinRAR is a file compressor / archiver great for backing up data. Download WinRAR at rarlab.com/download.htm.
WinRAR Tip #7: Batch File Backups:
Customize and make complex backups more efficiently with batch files.
For example, use one of these convenient batch files:
Windows version WinRAR batch file:
REM Windows Version
"C:\Program Files\WinRAR\WinRAR.exe" a -esh -m5 -hpYOURPASSWORD -mt2 -r -s -t "YOURARCHIVE.rar" @"files_to_include.txt" -x@"files_to_exclude.txt"
pause
Command line version WinRAR batch file:
REM Command Line Version
"C:\Program Files\WinRAR\RAR.exe" a -esh -m5 -hpYOURPASSWORD -mt2 -r -s -t "YOURARCHIVE.rar" @"files_to_include.txt" -x@"files_to_exclude.txt"
pause
They backup all files listed in files_to_include.txt but not including all files listed in files_to_exclude.txt.
The archive will be password protected with "YOURPASSWORD" as the password, although you can use the -p switch without specifying a password. If you do, WinRAR will ask you for it. If possible, I recommend not including your password so that it is not recored on your computer (inside the batch file). As previously mentioned, do not use "YOURPASSWORD" as your password! Visit GRC's Ultra High Security Password Generator for tips on secure password generation.
Let me break down the batch file for you
- REM stand for "remarks" and is how you provide commenting. Anything after "REM" is ignore during execution. In the examples above, I labelled the windows and command line versions using the REM command.
- "C:\Program Files\WinRAR\WinRAR.exe" is the full path to the Windows version of WinRAR. Change this if you have installed it in a different location.
- "C:\Program Files\WinRAR\RAR.exe" is the full path to the command line version of WinRAR. Change this if you have installed it in a different location.
- a stands for "add to archive" which tells WinRAR to wish to make an archive as opposed to extracing files from an archive or many other things it can do.
- -m5 sets the compression method. -m5 is the best (and slowest) compression. -m3 is the normal and default compression. -m1 is the fastest (least compressive) compression. -m0 sets no compression. See WinRAR's help for more details.
- -hpYOURPASSWORD sets the password you wish to use for archives that are password protected. Replace YOURPASSWORD with your actual password. If you use -hp instead, with a blank password, WinRAR will ask you for it manually each time. I recommend using -hp with a blank password as having your password stored in a batch file is a security risk. Note, this switch is similar to -p, but switch -p encrypts only file data and leaves other information like file names visible. -hp encrypts all sensitive archive areas including file data, file names, sizes, attributes, comments and other blocks, so it provides a higher security level. Without a password it is impossible to view even the list of files in archive encrypted with -hp.
- -mt2 sets the number of threads to 2. You can set the value from 0 to 16. -mt0 makes WinRAR use its single threaded algorithm. -mt1 to -mt16 makes WinRAR use its multi-threaded algorithm with as many threads as you specified. The multi-threaded algorithm has slightly different compressions than the single threaded algorithm. Use this if you have a multi-processor or hyper-threaded CPU, and set the value to the number of logical processors you have. Also see our WinRAR Tip #5: Run with Multi-Threading tip in this same article. If you do not know what you have, then do not use this switch. Note that using more threads affects the compression ratio, so the same archive created with different amount of threads will produce slightly different archives.
- -r sets WinRAR to recurse subfolders.
- -s sets WinRAR to create a solid archive. A solid archive is less secure as it each file is archived as a whole. This means greater compression but less stability in the archive. If your archive file becomes slightly corrupted, the entire archive will be unextractable with solid archiving. Without solid archiving, only the files stored in the section of the file that is corrupted will be lost. (You may also wish to see the data recovery -rr switch if you use solid archiving. See WinRAR help files for details.) A solid archive can have incredible compression rates if you are compression a lot of similar files. I have compressed 1.5GB of source code files down to less than 15MB with this option. It is incredible!
- -t tests files after archiving. I recommend this switch!
- "YOURARCHIVE.rar" is the filename of the RAR archive you are going to make. Name it whatever you wish.
- @FILENAME includes the files listed inside FILENAME in the archive. This is how you tell WinRAR what to archive. You can list files from all over your computer, just be sure to include the full path if the files are not found in the same folder as the batch file.
- -x@FILENAME excludes the file listed inside FILENAME from being archived. This is excellent for skipping unimportant files.
- -esh does not compress system (s) and hidden (h) files. The -e switch sets file exclude and include attributes. See WinRAR help files for more details and variations of this switch. This switch makes it convenient to create a WinRAR backup batch file that archives your home folder (e.g., C:\Documents and Settings\USERNAME\*.*) without archiving needless system and hidden files. That said, there are some system and hidden files that may be important to you, like Outlook Express email archives found in the hidden C:\Documents and Settings\USERNAME\Application Data\Identities folder.
Note that WinRAR commands do not use a dash (minus sign) and WinRAR switches require a dash (minus sign). To see more commands and switches, view WinRAR's help, found in the Help menu and named help topics.
Play around with it and test it out. Be sure to check the results to ensure you have not made any bugs. Do not just assume your backups work. Extract your backups and ensure they have everything you need so when the time comes to use them, they will do their job.
Use Batch File to Create Filename Using Date and Time:
In a batch file, you can use the %date% and %time% variables to access the date and time. Then you can extract just the letters and numbers you need. This allows you to create filenames from the current time and date, so it always creates a new filename that does not overwrite previous filenames. This is perfect for continual backups.
For example, if %time% equals "14:28:28.06", then %time:~0,2% equals "14". The "0" defines the position to start (the first string character is 0, not 1) and "2" defines the amount of characters to extract. So starting at position 0 and extracting 2 characters from "14:28:28.06" gives "14". Simple.
Try this simple batch file for various examples:
@ECHO OFF
REM %time% AND %date& SAMPLE OUTPUTS:
REM ---------------------------------
REM %time% example: 14:28:28.06
REM %date% example: Mon 03/26/2007
REM GET DATE / TIME:
REM (ensures no switch-over errors during parsing)
REM ----------------------------------------------
SET DATETIME=%date%%time%
REM PARSE DATE:
REM -----------------
SET YYYY=%DATETIME:~10,4%
SET MT=%DATETIME:~4,2%
SET DD=%DATETIME:~7,2%
REM PARSE TIME:
REM -----------------
SET HH=%DATETIME:~14,2%
SET MM=%DATETIME:~17,2%
SET SS=%DATETIME:~20,2%
REM REPLACE LEADING SPACE IN HOUR WITH 0 (IF LESS THAN 10)
REM ------------------------------------------------------
IF %HH% LSS 10 SET HH=0%HH:~1,1%
ECHO year = %YYYY%
ECHO month = %MT%
ECHO day = %DD%
ECHO hours = %HH%
ECHO minutes = %MM%
ECHO seconds = %SS%
ECHO format sample: %YYYY%-%MT%-%DD% %HH%.%MM%.%SS%s
pause
The output shouuld be something like this:
year = 2007
month = 03
day = 30
hours = 12
minutes = 42
seconds = 36
format sample: 2007-03-30 12.42.36s
Press any key to continue . . .
The above batch file example extracts various sections of the time and date strings. You can combine them together as you wish. A good example combination would be YYYY-MM-DD to ensure the filenames sort properly.
All WinRAR Tips:
- WinRAR Tip #1: Change Default Settings
- WinRAR Tip #2: Automatically Test Archived Files
- WinRAR Tip #3: Delete Multiple Files out of Multiple RAR Files
- WinRAR Tip #4: Run in Low Priority
- WinRAR Tip #5: Run with Multi-Threading
- WinRAR Tip #6: Make Additional WinRAR Copies Wait Their Turn
- WinRAR Tip #7: Batch File Backups
Have Any Tips To Add?
If you have WinRAR tips you would like to add, please contact us.
Also See:
External Links:
- RARLAB (the makers of WinRAR)
- WinRAR (Wikipedia)
- RAR (file format) (Wikipedia)
- Maximum Compression (shows maximum achievable compression ratio of 150+ compression programs )
- WinRAR (the company) (official publisher of RARLAB products, including WinRAR)
About the Author: I am Matthew Doucette of Xona Games, an award-winning indie game studio that I founded with my twin brother. We make intensified arcade-style retro games. Our business, our games, our technology, and we as competitive gamers have won prestigious awards and received worldwide press. Our business has won $190,000 in contests. Our games have ranked from #1 in Canada to #1 in Japan, have become #1 best sellers in multiple countries, have won game contests, and have held 3 of the top 5 rated spots in Japan of all Xbox LIVE indie games. Our game engines have been awarded for technical excellence. And we, the developers, have placed #1 in competitive gaming competitions -- relating to the games we make. Read about our story, our awards, our games, and view our blog.