Winrar command keys. General command line syntax. How to create a bat file and archive a folder in rar

A number of OS X users have difficulties working with RAR archives. Someone solves them by installing free utilities from the AppStore, but there is a better solution.

In general, this note is for those who do not want to load their Mac with unnecessary software and know how to use the terminal.

The author of the WinRar archiver actually did not deprive other OSes of attention. Versions for Linux, FreeBSD and Mac OS X are available for download on the official website, they just do not have a graphical interface.

Download the archive for OS X and unpack it into any convenient folder.

Installation

In order to use the commands for archiving rar and unpacking unrar, they need to be installed. This is done as follows:

The command transfers the rar application to the /bin folder of the system: sudo install -c -o $USER rar /bin The command transfers the unrar application to the /bin folder of the system: sudo install -c -o $USER unrar /bin

or for El Capitan and higher:

The command transfers the rar application to the /usr/local/bin folder of the system: sudo install -c -o $USER rar /usr/local/bin The command transfers the unrar application to the /usr/local/bin folder of the system: sudo install -c -o $ USER unrar /usr/local/bin

That's all. Commands will now be available from any directory, no matter where you are.

You can check if everything is fine by simply running rar or unrar in the terminal. As a result, you should see the version number and other information!

And yet, below we have collected several typical examples of using the archiver via the command line on OS X.

Examples of using

Creating a catalog archive:

Rar a archive.rar folder/

Creating an archive from several files:

Rar a archive.rar file.jpg file.doc file.mov

Creating an archive of the directory folder with the password qwerty:

Rar a -pqwerty archive.rar folder/

Creating an archive with a password, encryption and a list of files:

Rar a -hpqwert archive.rar folder/

Creating an archive without compression algorithms:

Rar a -m0 archive.rar folder/

Creating an archive with maximum compression:

Rar a -m5 archive.rar folder/

Unpacking the archive into the current directory:

Unrar e archive.rar

Unpacking the archive into a separate directory:

Unrar x archive.rar

That's all. Thank you for reading to the end!

Updated February 26, installation on OS X El Capitan and higher. Thanks for your comments e33 And neprokin!

rar.exe command options

http://www.forum.mista.ru/topic.php?id=227495

WinRAR can be controlled from the command line. General syntax command line is: WinRAR<команда> –<ключ1> –<ключN> <архив> <файлы…> <@файл-список…> <путь для извлечения\>command A combination of characters indicating the action that WinRAR will perform. key Keys are used to define specific actions, compression ratio, archive type, etc. archive The name of the archive being processed. files Names of the files being processed. list file List files are simple text files containing the names of the files to be processed. Each file name must be on a separate line and begin at the first position of the line. It is allowed to place comments in the list file; the sign of the beginning of a comment is the symbols //. For example, to archive *.txt files from the c:\work\doc folder, *.bmp files from the c:\work\image folder, and all files from the c:\work\misc folder, you can create a backup.lst containing the following lines: c:\work\doc\*.txt //backup copy of textsc:\work\image\*.bmp //backup copy of picturesc:\work\miscAfter this, for archiving it will be enough to run the command:rar a backup @backup.lstIn one The command line is allowed to specify both ordinary names or groups of files to process, and list files. If neither files nor file lists are specified, then the *.* pattern is assumed (i.e. WinRAR will process all files). extraction path Used only with the e and x commands and specifies the folder where files should be extracted. If this folder does not exist, it will be created. Notes a) If neither files nor a file-list are specified, then the *.* pattern is assumed, and WinRAR will process all files. b) If the archive extension is not specified, then WinRAR will use the default archive format selected in the default archiving profile, but if you want to specify the archive type explicitly, this can be done by adding the RAR or ZIP extension to the archive name. c) Keys entered on the command line have higher priority than the corresponding settings in the configuration dialog, i.e. the parameters specified by the keys are used. d) For commands c, e, s, t, rr, k and x, templates can be used in the archive name, so one command can process several archives at once. Moreover, if you specify the -r switch along with these commands, then the archives will be searched in all subfolders. e) Some commands and keys apply only to RAR archives, some to RAR and ZIP archives, and some to archives of all supported formats. This depends on the capabilities included in the archive format. f) Commands and switches are not case sensitive, so you can type them in both lowercase and uppercase letters.

Options:

Ac Remove the "archive" attribute after archiving or extracting -ad Add archive name to destination path -af<тип>Specify the archive format -ag[format] Add the current date and time to the archive name -ao Add files with the "archive" attribute set -ap Set the path inside the archive -as Synchronize the contents of the archive -av Add an electronic signature -av- Prohibit adding/verifying electronic signatures -cl Convert file names to lower case -cu Convert file names to upper case -df Delete files after archiving -dh Open shared files -ds Do not sort files when archiving -ed Do not add empty folders -en Do not add an "end of archive" block " -ep Exclude paths from names -ep1 Exclude base folder from path -ep2 Save full file paths -f Refresh files -hp[password] Encrypt both data and file headers -ibck Run WinRAR as a background process in the system tray -ieml Send archive by email -ilog[name] Write the error log to a file -inul Do not display error messages -k Lock the archive -kb Save files extracted with errors to disk -m Set compression method -mc<параметры>Specify additional compression options -md Set dictionary size -ms Specify file types for uncompressed archiving -os Save NTFS streams -ow Process file permissions information -o+ Overwrite existing files -o- Do not overwrite existing files -p[password] Set password -r Recursively with nested ones folders -r0 Recursively with subfolders using a pattern -rr[N] Add recovery information -rv[N] Create recovery volumes -s Create continuous archive -s Create contiguous groups using the file counter -se Create contiguous groups using file extensions -sfx[name] Create a self-extracting archive -sv Create independent contiguous volumes -sv- Create dependent contiguous volumes -s- Disable the creation of contiguous archives -t Test files after archiving -ta<дата>Process files modified after the specified date -tb<дата>Process files modified before the specified date -tk Preserve the original archive time -tl Set the archive time to the newest file -tn<время>Process files no older than the specified time period -to<время>Process files older than the specified time period -u Update files -v Create a multi-volume archive -vd Clean up a removable disk before archiving onto it -ver File version control -vn Use old scheme naming volumes -vp Pause before each volume -x<файл >Do not process the specified file -x@<файл-список>Do not process files specified in the list file -y Answer "Yes" to all requests -z<файл>Read archive comment from file -- Interrupt further search for keys on the command line

Teams:

A Add files to the archive C Add an archive comment D Delete files from the archive E Extract files from the archive, ignoring paths F Refresh files in the archive K Lock the archive M Move files and folders to the archive R Restore a damaged archive RC Recreate missing volumes RR[N] Add recovery information RV[N] Create volumes for recovery S[name] Convert the archive into a self-extracting one S- Remove the SFX module T Test the files in the archive U Update the files in the archive X Extract files from the archive with full paths

L: View archive contents.

Our examples:

Add to archive named arh.rar All files with extensions .dbf and .cdx.

1. Create a subdirectory Arhiv in the BAZA directory.

2. Set the path to the Arhiv subdirectory:

SET DEFAULT TO "D:\BAZA\Arhiv"

RUN WinRAR\RAR.exe a arh.rar D:\BAZA\*.dbf

RUN WinRAR\RAR.exe a arh.rar D:\BAZA\*.cdx

Extract files from archive, ignoring paths ( All files with extensions .dbf and .cdx):

1. Create a Razarh subdirectory in the BAZA directory.

2. Set the path to the Razarh subdirectory:

SET DEFAULT TO "D:\BAZA\Razarh"

3. Place the WinRAR program in the Arhiv directory.

4. Enter MS-DOS command line commands:

RUN WinRAR\unrar.exe e -y arh.rar *.dbf "D:\BAZA\razarh"

RUN WinRAR\unrar.exe e -y arh.rar *.cdx "D:\BAZA\razarh"

Archiving files

Archive programs running under Windows 9x

Archiving files When working on personal computer there is often a need for long-term storage backup copies files on floppy disks. When you copy files, the copies take up as much space as the original files, so you may need many floppy disks. For such cases, special archiving programs. These programs produce compression, packaging data, which leads to space savings of 40-50%. In addition, when archiving, files can be combined into one block called archive file or simply archive, which is typically done when storing groups of shared files. After archiving, the resulting archive files can be treated like any other: copied, deleted, renamed. The archive file contains, in addition to the archived files, also a list of them (archive table of contents) and control codes for each file to check the integrity of the archive.

If necessary, the required archive file is unzipped, unpacked using decompressor program(often the archiving and unarchiving functions are combined in one program), after which the archive turns into the same source file or group of files that were placed in it during archiving.

Let us introduce the concept “ base" catalog. “Base” will be the directory (folder) that contains the files to be archived or into which the files should be placed after unpacking the archive.

All files contained in the base directory (folder), and also, if necessary and if the appropriate command format is given, and in its subdirectories, when archiving, are converted into one archive file or can be added to an already existing archive file. When you unzip an archive file, all the files it contains are placed in the base directory, and, if necessary, the required subdirectories can be automatically created in it.

“By default,” all files from the specified directory are included in the archive. If only part of them needs to be included in the archive, an explicit indication of the file name or list of names is used. In this case, you are allowed to use MS-DOS templates to specify a group of files.

Modern archiving programs must perform the following basic functions:

    create archive files from individual or all files of the base directory and its subdirectories;

    add and replace files in the archive;

    extract and delete files from the archive;

    view the contents of the archive;

    protect each of the archived files with code;

    create multi-volume archive, i.e. an archive consisting of several files that will be located on different floppy disks (to denote storage media - individual floppy disks - it is customary to use the term “ volume”);

    test the archive, checking the safety of information in it;

    enter comments to files into the archive;

    remember file paths in the archive;

    create self-extracting archives both on one volume and on several volumes. Self-extracting the archive is created with the extension .exe, which contains an unpacking software module, so no unzipping program is required to extract files from it;

    view the contents of text files contained in the archive;

    save multiple versions of the same file in an archive;

    restore files from destroyed archives;

    ensure the protection of information in the archive and access to files placed in the archive using a password.

Archive programs work separately under MS-DOS and under Windows 9x.

Archive programs running under MS-DOS

Currently, there are several dozen of them in the world, the most common are shown in Table 1

Table 1

The name of the program

Brief description of the program

Created in 1993 by Robert K. Young (USA). This program is the standard of archiving programs.

Created in 1994 by Evgeny Roshal (Ekaterinburg, Russia). The only program in the world that contains a built-in shell like Norton Commander. In addition, if there are appropriate archivers on the disk, it processes archives of many common formats.

Software package ZIP

Contains the PKZIP.EXE archiver and the PKUNZIP.EXE unarchiver. Very good at compression graphic information. Created in 1993 by PKWARE Inc. (USA).

Archiver programs get their names from the extensions assigned to archive files. The differences between archiver programs lie in the speed of operation, the compression density of archives, and the algorithms for compressing files into an archive.

The principles of using most archiving programs running under MS-DOS are similar. The differences come down mainly to the format of the help information displayed on the screen and the format for recording commands.

To correctly record the command to call the desired archiver program, you must use hint text. Help text information is usually displayed on English language and contains examples of composing commands to perform various operations. Examples of calling the help text of the archiver programs given in Table 1 (for clarity, the commands are written in capital characters):

1. For the archiver program arj.exe:

ARJ.EXE /? ,

2. For the archiver program rar.exe:

RAR.EXE /? ,

Where /? – parameter for receiving a full hint.

3. For the archiver program pkzip.exe:

PKZIP.EXE /h .

For the decompressor program pkunzip.exe:

PKUNZIP.EXE /h ,

where /h is the parameter to get a full hint.

The tooltip indicates the MS-DOS command line syntax, a list of commands and options available in this program.

Command line syntax of MS-DOS archive programs:

< archiver program name><команда> [ option...]< archive file name>[ <file names>]

Where<name of the archiver program> - name launched archiver program or dearchiver program;

<team> - a symbol indicating to the archiver which of the operations is required in this case;

[ option...] - a setting option, also called an “extension” or “key” of a command and consisting of one or more characters preceded by either the character “ / ”, or the symbol “ - " A command can have multiple options, written with a space after the command character;

<archive file name> - the archive file being processed is specified with the extension of the corresponding archiver program, for example: . arj, .rar, .zip ;

<file names> - sets the names of the files that are included in the archive file. File names are listed separated by spaces. If there are no file names, then they are processed All files of the current subdirectory.

Parameters enclosed in square brackets are optional. Thus, the options and the list of archived files are optional parameters and are not always used. In some cases, the command may not be specified, in which case the operation is considered to be specified by default.

Let's look at the features of archiver programs ARJ, RAR, PKZIP and PKUNZIP which are known as the best in terms of the range of functions provided to the user, compression ratio and operating speed.

  • Introduction
  • How the backup will work
  • Scripts
  • WinRAR command line
  • Batch files
  • Scheduler
  • Conclusion
  • Links

Introduction

Backups should always be made. Especially for servers. This is an axiom. There are different ways to backup - just copying, archiving, many specialized software(including, for example, standard backup from Microsoft), working with disks, tapes, MO, etc. But what to do if third-party software cannot be installed, and the capabilities of the standard one are not enough? For example, you want to have a backup of some valuable server folders, in automatic mode, for, say, the last week? Write your own software? Can. But not hunting. From a cannon at sparrows. I'd like to make do with just a script. And a good archiver will help us with this. WinRAR, built-in Windows scheduler and book Jesse Torres "Scripts for Windows administrator ".

How the backup will work

So, I need:

  • Store a backup for each day as an archive file
  • Each file should be created with a unique name (ideally include the creation date in the file name)
  • The backup creation process should be automatic
  • Files older than 1 week should be automatically deleted

Archiving will be handled WinRAR, it will automatically name the files. The script will determine the age and delete old files ( VBScript), and I will automate everything using the built-in Windows Scheduler.

Scripts

The scripts are based on procedures from the book Jesse Torres "Windows Administrator Scripts "(specifically from chapter 3 "File Serving"). They were supplemented by me to implement my tasks.
Here are the functions we need (here is the entire code):

" ---------- " Start of the main section. Execution will begin from here when the script starts. Set FSO = CreateObject("Scripting.FileSystemObject") " directory where backups will be stored. You must enter your directory " instead of "X:\DailyBackup" sDir = "X:\DailyBackup" Set objDir = GetFolder(sDir) DeleteOlderFiles(objDir) " ---------- Section functions "Get file Function GetFile(sFile) On Error Resume Next Set FSO = CreateObject("Scripting.FileSystemObject") Set GetFile = FSO.GetFile(sFile) if err.number 0 then WScript.Echo "Error Opening file " & sFile & VBlf & "[ "&Err.Description&"]" Wscript.Quit Err.number end if End Function "Get folder Function GetFolder (sFolder) On Error Resume Next Set GetFolder = FSO.GetFolder(sFolder) if err.number 0 then WScript.Echo "Error Opening folder " & sFolder & VBlf & "["&Err.Description&"]" Wscript.Quit Err .number end if End Function " delete one file (the file name is passed to sFile) Sub DeleteFile(sFile) On Error Resume Next FSO.DeleteFile sFile, True if err.number 0 then WScript.Echo "Error Deleteing file " & sFile & VBlf & "["&Err.Description&"]" Wscript.Quit Err.number end if End Sub " Delete files older than 7 days Sub DeleteOlderFiles(objDir) "look through all the files in the directory for each efile in objDir.Files " we use DateLastModified rather than DateCreated because " DateCreated does not always return the correct date FileDate = efile.DateLastModified Age = DateDiff("d",Now,FileDate) " in this case, the file is no more than seven days old If Abs(Age)>7 Then DeleteFile(efile) End If next End Sub

We put this code in the file remove_old_backups.vbs.

WinRAR command line

WinRAR archiver has powerful command line support. In particular, it can generate a file name based on a custom template, including various service information, for example, a date. Which, in fact, is what we need. In addition, it can work with a list of files or directories for archiving, which makes editing a batch file much easier.

I use the following command line for archiving (written in the make_daily.bat file):

"c:\program files\winrar\winrar.exe" a -s -r -rr10p -dh -ed -m3 -ilogC:\DailyBackupScript\dailybackup.log -ag+YYYYMMDD X:\DailyBackup\backup

Let's break it down into components:

  • "c:\program files\winrar\winrar.exe"- path to WinRAR
  • a- command to add files
  • -s- compression mode " solid"
  • -r- with subdirectories
  • -rr10p- just in case, we add information for recovery (-rr), in the amount of 10% (10p)
  • -dh- open the halyards. Useful if someone stayed up until 12 am and edits a file that should go into backup
  • -ed- do not add empty directories
  • -m3- compression ratio 3
  • -ilogC:\DailyBackupScript\dailybackup.log- keep a log (-ilog) and write it to "C:\DailyBackupScript\dailybackup.log"
  • -ag+YYYYMMDD X:\DailyBackup\backup- form the file name by adding the current year, month and day to the beginning. The file name will look something like this: 20060927backup.rar
  • X:\DailyBackup\backup- the folder where to write backups and the name of the backup (to which the year, month, day are added to the beginning)

Batch files

All work on creating a backup is based on command files (.bat files). I have two of them:

  • make_daily.bat- contains commands for creating an archive file or files.
  • backup_task.bat- performs the following actions:
    • calls the script interpreter cscript and pass it a script to execute that deletes old files.
    • causes make_daily.bat and passes it as a parameter either the folder that needs to be archived or a list file (which contains several folders for archiving).
  • In its turn, backup_task.bat called Windows Scheduler at the designated time

File contents make_daily.bat(in one line):

@"c:\program files\winrar\winrar.exe" a -s -r -rr10p -dh -ed -m3 -ilogC:\DailyBackupScript\dailybackup.log -ag+YYYYMMDD X:\DailyBackup\backup %1

File contents backup_task.bat:

@cscript remove_old_backups.vbs call make_daily.bat @backup.lst

To work you also need to create a file backup.lst(plain text file). It should contain a list of directories that are included in the backup. Each directory should be located on a separate line, for example, like this:

C:\Projects c:\VeryImportantProjects c:\Folder Name With Spaces\SubFolder

Please note that the source directory name containing spaces does not need to be enclosed in quotes if you are using a list file. If you pass it directly, then quotes are required. Don't put a trailing slash - WinRAR won't understand this.

Scheduler

The scheduler can be called through the menu Start|Programs|Accessories|System Tools|Scheduled Tasks, or through Control Panel. The folder will open Scheduled Tasks, to which you need to add a new task. This can be done as follows (I only give the basic steps).

Double click Add Scheduled Task to add a new task

Let's select our bat file:

Set the name and frequency of the task

Let's set the start time

Let's set account and the password under which the task will be executed. Necessarily!

Task added

You can edit task properties by clicking right click mouse on the task icon in the folder Scheduled Tasks and selecting the menu item Properties.

Conclusion

So we created the simplest, but powerful :) “backup” (or backup? Which is correct?) from improvised means. It can be expanded and customized, changed archivers, added all sorts of checks, etc. And all this without using third-party products and almost without programming.
I hope my experience will be useful to you.

Command line syntax

WinRAR can be controlled from the command line. The general command line syntax is:

WinRAR<команда> -<ключ1> -<ключN> <архив> <файлы> <@файл-список> <путь для извлечения\>

team A combination of characters that defines the action that WinRAR will perform.
key Keys are used to determine specific actions, compression ratio, archive type, etc.
archive The name of the archive being processed.
files Names of processed files.
list file List files are regular text files containing the names of the files to be processed. Each file name must be listed on separate line and start from the first position of the line. It is allowed to place comments in the list file; the sign of the beginning of a comment is the // symbols. For example, to archive *.txt files from the c:\work\doc folder, *.bmp files from the c:\work\image folder, and all files from the c:\work\misc folder, you can create a backup.lst containing the following lines:

c:\work\doc\*.txt//backup copy of texts

c:\work\image\*.bmp//backup copy of pictures

After this, to archive it will be enough to run the command:

winrar a backup @backup.lst

On one command line you can specify both regular names or groups of files to process, and list files. If neither files nor list files are specified, then the template is assumed *.* (i.e. WinRAR will process all files).

extraction path Only used with commands e And x and specifies the folder into which the files should be extracted. If this folder does not exist, it will be created.

Notes

a) If neither files, neither list file, then the pattern *.* is implied , those. WinRAR will process all files.

b) If not specified extension archive, then WinRAR will use the default archive format selected in the default archiving profile, but if you want to specify the archive type explicitly, you can do this by adding to the name archive RAR or ZIP extensions.

c) Keys entered on the command line have higher priority than the corresponding settings in the configuration dialog, i.e. the parameters specified by the keys are used.

d) For teams C, E, S, T, R.R., K And X You can use wildcards in the archive name, so you can process several archives at once with one command. Moreover, if you specify the key along with these commands, then the archives will be searched in all subfolders.

e) Some commands and switches are applicable only to RAR archives, some to RAR and ZIP archives, and some to archives of all supported formats. This depends on the capabilities included in the archive format.

f) Commands and switches are not case sensitive, so you can type them in both lowercase and uppercase letters.