How to create a RAR archive with current date in archive file name?

Levan Berishvili picture Levan Berishvili · Aug 12, 2016 · Viewed 8.8k times · Source

I have folder D:\data with lots of subfolders and files and I want to archive this folder with a batch file to a specified directory with current date added in archive file name, e.g. F:\11.08.2016_data.

My command line is so far:

"C:\Program Files\WinRAR\rar.exe" a -ep1 -r "data" "D:\data"

This command line creates RAR archive file data.rar in folder My Documents.

How can I add date and change the archiving directory?

Answer

Mofi picture Mofi · Aug 12, 2016

How to build a WinRAR command line?

  1. Start WinRAR.
  2. Click in menu Help on Help topics.
  3. Click on tab Contents on list item Command line mode.
  4. Click on list item Command line syntax, read this help page and copy the bold line into a text editor or into a command prompt window.
  5. Click on tab Contents on list item Commands.
  6. Click on list item Alphabetic commands list and replace <command> in text editor or command prompt window by most suitable command letter for the task.
  7. Click on tab Contents on list item Switches.
  8. Click on list item Alphabetic switches list and replace -<switch1> -<switchN> in text editor or command prompt window by those switches which are useful for the task.
  9. Replace the rest of the WinRAR command line in text editor or command prompt window by the appropriate file and/or folder names.

How to build a RAR command line?

  1. Open the folder containing the program files of WinRAR.
    The standard folders are:
    %ProgramFiles%\WinRAR
    %ProgramFiles(x86)%\WinRAR
  2. Double click on the file Rar.txt to open it. This is the manual for Rar.exe, the console version of WinRAR.
  3. In chapter RAR command line syntax the general command line syntax is described which should be used as template for the command to enter in a text editor or in a command prompt window.
  4. Next the available commands are listed in alphabetical order with a brief description. One of these commands must be used for the Rar command line.
  5. Then the list of switches follows, also in alphabetical order and also explained. It is advisable to read about the switches and while reading typing the useful switches for the task in text editor or in command prompt window.
  6. Type the rest of the Rar command line in text editor or command prompt window, i.e. the file and folder names.

An appropriate Rar command line for your task would be:

"%ProgramFiles%\WinRAR\rar.exe" a -agYYYY-MM-DD -cfg- -ep1 -inul -m5 -r -y "F:\data_.rar" "D:\data\"

The switch -agYYYY-MM-DD is responsible for creating in directory F:\ archive files for example with name data_2016-08-11.rar.

It is of course also possible to use:

"%ProgramFiles%\WinRAR\rar.exe" a -agDD.MM.YYYY -cfg- -ep1 -inul -m5 -r -y "F:\data_.rar" "D:\data\"

This command line creates in F:\ archive files with name data_11.08.2016.rar. But this is not advisable as the international date format YYYY-MM-DD has the advantage that the files listed alphabetically sorted by name are automatically also listed by date which is not the case with date format DD.MM.YYYY.

See the answer on Simply compress 1 folder in batch with WinRAR command line? for difference on being specified D:\data or D:\data\ on Rar command line.

A + must be inserted between -ag and date format string for date left to data separated with an underscore in archive file name.

Date in international format YYYY-MM-DD at beginning of RAR file name:

"%ProgramFiles%\WinRAR\rar.exe" a -ag+YYYY-MM-DD -cfg- -ep1 -inul -m5 -r -y "F:\_data.rar" "D:\data\"

Date in format DD.MM.YYYY at beginning of RAR file name:

"%ProgramFiles%\WinRAR\rar.exe" a -ag+DD.MM.YYYY -cfg- -ep1 -inul -m5 -r -y "F:\_data.rar" "D:\data\"