问题
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:
How to build a WinRAR command line?
- Start WinRAR.
- Click in menu Help on Help topics.
- Click on tab Contents on list item Command line mode.
- 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.
- Click on tab Contents on list item Commands.
- 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. - Click on tab Contents on list item Switches.
- 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. - 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?
- Open the folder containing the program files of WinRAR.
The standard folders are:%ProgramFiles%\WinRAR
%ProgramFiles(x86)%\WinRAR
- Double click on the file
Rar.txt
to open it. This is the manual forRar.exe
, the console version of WinRAR. - 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.
- 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.
- 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.
- 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\"
来源:https://stackoverflow.com/questions/38908868/how-to-create-a-rar-archive-with-current-date-in-archive-file-name