'How to compress each subfolder in a folder into separate RAR archives with rar.exe using a batch script?

I have multiple folders inside N:\working.

  • N:\working\1
  • N:\working\2
  • N:\working\3

I want to RAR compress them all individually on 64-bit Windows.

Here is the code I have so far:

@ECHO OFF
CD N:\working

SET rar_executable=C:\Rar.exe
ECHO  Using WinRAR executable: %rar_executable%

ECHO.
%rar_executable% a -r -ep1 -m0 "Test.rar"

pause

It produces the file Test.rar containing all three folders. But I want to archive the three folders into the archive files 1.rar, 2.rar and 3.rar.



Solution 1:[1]

That task can be done with a batch file containing only a single command line.

@for /D %%I in ("N:\working\*") do @"%ProgramFiles%\WinRAR\Rar.exe" a -cfg- -ep1 -inul -m5 -r -- "%%I.rar" "%%I\"

The same command line for usage in a command prompt window without using a batch file:

for /D %I in ("N:\working\*") do "%ProgramFiles%\WinRAR\Rar.exe" a -cfg- -ep1 -inul -m5 -r -- "%I.rar" "%I\"

There is no @ left to for as that would be completely useless on running this command line in a command prompt window. @ left to Rar.exe with full qualified file name is removed to see the command line output by cmd.exe when a new archive file creation starts.

FOR searches in specified directory N:\working for non-hidden subdirectories and assigns the full qualified directory name to loop variable I before executing Rar.exe for this subdirectory.

Rar.exe adds (a) recursively (-r) all directories and files in the subdirectory found by FOR to a RAR archive file with name of the current subdirectory and file extension .rar created in the specified directory N:\working using best compression (-m5) without any output to console window and ignoring all errors (-inul) which could occur during folder compression. The standard configuration is ignored (-cfg-) on compression. The name of the compressed subdirectory with its path is not added to the RAR archive file (-ep1 and folder to compress ends with a backslash).

Open a command prompt window, run for /? and read the output help explaining option /D (directory search).

Open text file Rar.rxt in program files folder of WinRAR with a double click which is the manual of Rar.exe explaining the command a and the switches used here.

This task could be also done with WinRAR.exe using a profile created before with starting WinRAR.exe with the profile name as only option on the command line stored in a shortcut file. This solution would not open a console window because of WinRAR.exe is a Windows GUI desktop application.

Solution 2:[2]

try this script as a .bat file which will compress each folder as a RAR file within the current directory and also will show some additional detail which is very useful while compressing a large size of data.

.Bat File Script

@echo off
@for /D %%I in (".\*")  do echo started at %date% %time% compressing... "%%I" && @"%ProgramFiles%\WinRAR\Rar.exe" a -cfg- -ep1 -inul -m5 -r -- "%%I.rar" "%%I\" 
echo "Completed Successfully !!!"
pause

Sample Outout

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Haseeb