Simply compress 1 folder in batch with WinRAR command line?

Use either

"%ProgramFiles%\WinRAR\Rar.exe" a -ep1 -idq -r -y "Name of RAR file with path" "%UserProfile%\Desktop\someFolder"

or

"%ProgramFiles%\WinRAR\Rar.exe" a -ep1 -idq -r -y "Name of RAR file with path" "%UserProfile%\Desktop\someFolder\"

to create a RAR archive file with the specified name after command a (add to archive) and the switches

  • -idq … enable quiet mode to display only error messages,
  • -ep1 … exclude base directory from specified file/folder names,
  • -r … recursively archive/compress all files and subdirectories,
  • -y … assume Yes on all queries.

The folder someFolder is included in the archive with first command line without a backslash at end.

The folder someFolder is NOT included in the archive, just the files and the subdirectories of this folder, with second command line with the backslash at end.

In other words option -ep1 results in omitting everything from path up to last backslash in specified file or folder name on adding the file or folder to the archive which explains the difference on adding a folder without or with backlash at end specified on command line.

Recursion means to add not only the files in the specified folder, but also all subfolders and all files in all subfolders.

So RAR must search first in the specified folder for a subfolder. If found go into this subfolder and search again for a subfolder. If one is found, go into this subfolder and search for a subfolder. If no one found, add the files in this subfolder into the archive or just the folder name if the subfolder is empty. Then go back to parent folder and continue searching for next subfolder. If none is found, add the files of this subfolder. Then go back to parent folder and continue search for subfolder, and so on.

As you can read, the same procedure is done again and again for each branch of the entire folder tree until all subfolders were processed. This is done using a recursion. The subroutine searching for subfolders calls itself every time a subfolder is found.

NOTE:

Console version Rar.exe supports only creation/extraction of RAR archives. It does not support ZIP archives. This is clearly written in text file Rar.txt at top which is the manual for console version of WinRAR. It would be necessary to use WinRAR.exe instead of RAR.exe to create ZIP archives.

Example 1:

"%ProgramFiles%\WinRAR\WinRAR.exe" a -afzip -ep1 -ibck -r -y "Name of ZIP file with path" "%UserProfile%\Desktop\someFolder"

Example 2:

"%ProgramFiles%\WinRAR\WinRAR.exe" a -afzip -ep1 -ibck -r -y "Name of ZIP file with path" "%UserProfile%\Desktop\someFolder\"

GUI version WinRAR.exe has many commands and switches identical to console version Rar.exe, but there are differences as shown here with -afzip supported only by WinRAR.exe and -ibck instead of -idq to run WinRAR in background which means minimized to system tray instead of in foreground with a visible progress window.

For help on creating WinRAR.exe command line start WinRAR, click in last main menu Help on first menu item Help topics, select help tab Contents, expand list item Command line mode and make use of the help pages:

  • Command line syntax
  • Alphabetic commands list
  • Alphabetic switches list

It is advisable to read the help pages in listed order respectively the text file Rar.txt from top to bottom on creating the WinRAR.exe or Rar.exe command line for usage in a batch file or in a shortcut file (*.lnk).

Leave a Comment