How to create a RAR archive with current date in a

2019-07-24 08:29发布

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?

1条回答
神经病院院长
2楼-- · 2019-07-24 09:16

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\"

to create 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.

For date string left to data separated with an underscore in archive file name it would be necessary to use a batch file which additionally renames the created RAR archive file using command REN.

@echo off
setlocal EnableDelayedExpansion

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

for %%I in (F:\data_*.rar) do (
    set "RarFileName=%%~nI"
    set "NewFileName=!RarFileName:~5!_data"
    ren "%%~fI" "!NewFileName!%%~xI"
)

endlocal

For understanding the used commands in the batch code above and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo /?
  • endlocal /?
  • for /?
  • ren /?
  • set /?
  • setlocal /?
查看更多
登录 后发表回答