This guide describes the use of the WinRAR command-line tools for compressing and uncompressing files in a directory and their use in batch files. This guide is an extension of a previous post, Automate Zipping Tasks using the Command-line Interface that covered the use of the command-line tools for two free compression utilities, IZArc and 7-Zip. The information was tested on a Windows PC running Vista.

compression represented by a vise

WinRAR is a popular and powerful archive manager that can be used from the command-line or with scripting languages such as batch files. It includes two command-line tools, rar.exe and unrar.exe, where rar.exe compresses and unrar.exe uncompresses files. Both are located in the “C:\Program Files\WinRAR” folder in the installable version. Although WinRAR is shareware, it can be used on a trial basis for 40 days. Using WinRAR’s command-line tools is similar to those for IZArc and 7-Zip. The syntax for using the WinRAR executables is:

WinRAR <command> -<switch1> -<switchN> <archive> <files...> <@listfiles...> <path_to_extract\>

Examples to compress a folder:

rar a -r yourfiles.rar *.txt c:\yourfolder

creates archive yourfiles.rar and compresses all .txt files in c:\yourfolder and all its subfolders

rar a yourfiles 

creates archive yourfiles.rar and compresses all files in the current folder, but doesn’t include subfolders (note lack of extension; WinRAR will use the default extension .rar)

“a” command adds to the archive

“-r”  switch recurses subfolders

Examples to uncompress a folder: 

unrar x c:\yourfile.rar *.gif c:\extractfolder\

extracts all *.gif files from yourfile.rar to c:\extractfolder\ (trailing backslash required) and restores the folder structure

unrar e c:\yourfile.rar 

extracts all files in c:\yourfile.rar to the current folder (folder structure ignored)

“x” command extracts with full paths

“e” command extracts and ignores paths

Example using Multiple Switches:

rar a -r -u -df -x*.bat earchive.rar c:\test\*.*

compresses all new or updated files from c:\test and its subfolders to earchive.rar, deletes the files after they are added to the archive, and excludes any files with a “bat” extension,

“a” command adds to the archive

“-r”  switch recurses subfolders

“-u” switch. Equivalent to the “u” command when combined with the “a” command. Adds new files and updates older versions of the files already in the archive

“-df” switch deletes files after they are moved to the archive

“-x” switch excludes the specified file(s) from the operation

Basic rules for WinRAR:

  • When files or listfiles are not specified, all files in the current folder are processed
  • When specifying all files in a folder, yourfolder or yourfolder\*.* are equivalent
  • Folder structures are automatically saved in archives (but not automatically extracted)
  • WinRAR uses the .rar extension by default, but that can be overridden by specifying the zip extension in the archive name
  • Switches and commands are not case sensitive and can be written in either upper or lower case

Another point is that WinRAR doesn’t appear to use the Windows path environment variable, so it must be specified either at a command prompt, set permanently in the environment variable settings, or specified in a batch file.

To set the Windows path environment variable temporarily at a command prompt or in a batch file, use the following command:

set path="C:\Program Files\WinRAR\";%path%

To set it permanently in the Windows path for your PC:

start–>Control Panel–>System–>Advanced system settings–>Advanced Tab–>Environment Variables–>System Variables–>Path–>Edit. Add the path ;C:\Program Files\WinRAR; to the end (don’t forget the single semicolons at the beginning and end). Hit OK three times.

Using WinRAR in Batch Files:

Two batch file examples are provided, The first (compress_rar_rev1.bat) provides several compression options such as such as compressing all files in a folder or a folder and its subfolders, with options to compress them into a single archive or individually and to include or exclude paths. The second batch file (uncompress_rar.bat) decompresses all .rar files from a folder and places the extracted files into another directory. Be sure to change the extension(s) to .bat before using either file. Both of the following batch files temporarily set the Windows path environment variable for the WinRAR folder when executed.

compress_rar_rev1.bat

uncompress_rar.bat

 

Share

11 Comments

  1. rf_tremalnike says:

    Hi, thank you for your attention. The rev1 script is working well, but put all new archives in parent directory,not in subdirectory where source files are located. I do some little changes to frist version, and now it’s works for me (I’m using -df switch also):
    FOR /R %%i IN (*.*) do (rar a -m5 -ep “%%i.rar” “%%i”)
    Now,the only one problem is that new archive file name include source file type.
    For example archive of “test.doc” looks like: test.doc.rar” instead “test.rar”
    Best Regards !

    • admin_tb says:

      Your original command doesn’t seem to work. I’m not sure why it would work on your system. The following was tested and worked on XP SP3. The .rar extension is optional:
      FOR /R "%dir%" %%i IN (*.*) do (rar a -ep -m5 "%%~pi\%%~ni".rar "%%i")

      It compresses files individually within their folders from the default folder to all subfolders, including any folder names with spaces. The rev1 script has been updated with this information.

  2. rf_tremalnike says:

    Nice script ! Many thanks !
    Is there any way to modify “compress.bat” in way to compress files in folder and subfolders (also in separate rar files, in each subfolder) ?

    • admin_tb says:

      The file has been updated and posted as compress_rar_rev1.bat. It includes options for compressing the files in a folder and its subfolders in a single archive or individually.

  3. Pinso says:

    Thanks the cmd line worked perfectly, now i can make and put the comments on a rar file in a single bat file. Good blog, please keep it updated.
    Was looking everywhere for this.

  4. Pinso says:

    Hello, nice tutorial for Winrar from commandline, although i had to do some search for making an archive from command line not only from this site but from other sites too, i would like to know if their is some way of adding comments to rar files from the command line.
    Since 7.zip work only from the GUI, and i don”t use winzip from command line too much except Winrar and 7zip, since 7zip is out of question , so winrar might have a chance and i tried the -c and the -cf it didn’t work and i tried without the dash still no good.

    Does anyone have worlked around this.

    • admin_tb says:

      You can use rar.exe to add comments from the command-line non-interactively ONLY by using a comment file and using the “c” and “-z” switches, but this only works for *.rar files. This includes removing comments by using a blank file. I don’t see any information about the -c and -cf switches, at least in my version of WinRAR. To add comments to archives without user interaction with a comment file, use the following command syntax:

      rar c -zyourcommentfile.txt yourRARarchive

  5. David Frederiksen says:

    I have been using the cli for archiving for a while now but I have run into an issue with the temp directory or cache that it uses. The archive I am creating is quite large and I have now run out of cache/temp space to create the archive. Is there a way to turn off the caching or the need for the temp directory so that it is writing directly to the archive?

    Regards,
    Dave

    • admin_tb says:

      That’s a good question. I don’t think it’s possible for any of the familiar casual user-oriented type tools like WinZip, IZArc, etc. In fact, WinZip help states that it can’t execute at all without a temporary directory. There may be some commercial grade utilities that could accomplish this, but that’s a guess. The best bet would be to contact the developers at any of the popular archiving utilities and see what they have to say.

  6. Hostgator Coupons says:

    Thanks , great information , i want to remove comments from multiple archives through command line i didn’t know if this possible ?

    • admin_tb says:

      The only freeware tool that I’m aware of that can delete or modify archive comments (not an archive’s file comments) from the command line is Info-Zip’s zip.exe utility: http://downloads.sourceforge.net/gnuwin32/zip-3.0-bin.zip. However, this tool is interactive, so automating it fully would be impossible or difficult through a batch file, but it may be possible using other scripting languages. Also, it only works with *.zip files.

      Info-Zip’s command line utility automatically displays the current comment and prompts the user for input. To change the comment, enter your new comment, hit ENTER, enter a period (.) and then hit ENTER again. To remove a comment, enter a period (.) only and then hit ENTER. This process can be partially automated with a batch file where the batch file will step through every zip file in a specified directory and allow the user to remove the archive comments by entering a period (.) and then ENTER at the utility’s prompts (see sample batch file below):

      @echo off

      Setlocal Enableextensions
      Setlocal Enabledelayedexpansion

      REM zipcomments

      REM Use Info-Zip's zip.exe command line tool to insert blank/changed comments for all zip archives in the REM folder set below:

      REM ---------------Set working folder and zip.exe path-------------
      set dir=C:\Mydirectory\
      set path="C:\zip300xn\";%path%
      REM ---------------------------------------------------------------

      REM change to working directory specified above
      cd %dir%

      REM ------------------------- Program Code ------------------------
      echo.
      FOR %%i IN (*.zip) do (
      echo Editing comments for: %%~fi
      zip.exe -z "%%i"
      )

      echo.
      ENDLOCAL
      pause
      EXIT /B 0

      REM ------------------------- Program Code End --------------------

      Of course, you can always use the IZArc or WinZip GUIs to remove archive comments. WinRAR has a very powerful command line tool (rar.exe) to modify comments without requiring user interaction (it’s able to use a file), but it isn’t freeware, requires an external comment file, and limited testing indicates that it works only with *.RAR files. WinZip also has a sophisticated command line tool (wzzip.exe) that is interactive, although much less so than Info-Zip’s in that the user needs only to type in a comment and hit enter once for each *.zip file. However, WinZip’s tool is not freeware and it works only for *.zip files. The above batch file can be easily changed for use with either the WinRAR or WinZip command line tools.

      AFAIK, 7-Zip’s and IZArc’s command line utilities don’t have the capability to insert file or archive comments. 7-Zip can modify/delete FILE comments only through its UI, and IZArc can modify/delete ARCHIVE comments only through its UI.

Bear

Bad Behavior has blocked 367 access attempts in the last 7 days.