September 25, 2012Sometimes it's quite handy to be able to archive a project or folder into a date stamped archive without manually doing the job each and every time. Enter RAR and a bat-script. If you don't already have, download WinRAR from here.
Mission briefSo lets set up a bat-script to archive a folder and date stamp the archive. The mission brief is to backup all files within the D:\Projects\MyProject\Source\ folder, but exclude temporary files created by the ReSharper plugin.
Creating the required filesFirst we create a text file containing all the paths we want to archive. Notice that you don't need to specify sub folders as we'll use the recursive function when archiving. We'll put the files in the same folder which will act as a base when storing the paths, we'll use "D:\Projects\".
Now we create another text file, but this time with the paths within the one specified above that we don't want included in the archive, i e the ReSharper plugin working folder. If you want to exclude any other folders or files, here is where you want to add them.
Okay, lets create the actual bat file. You might want to change the path towards your WinRAR installation or the date stamp pattern. I use YYYYMMDDHHMM which gives (without the plus signs) year+month+day+hour+minute using two digits for each value (except year which uses four).
"C:\Program Files\WinRAR\rar.exe" a -agYYYYMMDDHHMM "MyBackupFolder\MyProject-Backup-" -ep1 -r -m5 -x@"exclude.lst" @"filelist.lst"
Explanation of the parametersa
Add files to archive
Generate archive name using the current date
Exclude base directory from names (we don't need to archive D:\Projects\, we're happy with archiving the content as MyProject\Source\...).
Set compression level (0-store...3-default...5-maximal) (we're using 5 in the example above)
The above script will create an archive in "MyBackupFolder" called "MyProject-Backup-201209251907.rar".