Skip to main content

How to Create a RAR Archive from a Batch Script

While ZIP and 7Z are incredibly common, the RAR archive format remains highly popular due to its advanced compression algorithms, solid archiving capabilities, and error recovery records. Natively, Windows does not support creating .rar files. You must use the official command-line utility provided by WinRAR, called Rar.exe.

In this guide, we will demonstrate how to automate the creation of RAR archives directly from a Batch script.

The Strategy: The Rar.exe Command-Line Tool

  1. Ensure WinRAR is installed on your machine.
  2. Locate the command-line executable (typically C:\Program Files\WinRAR\Rar.exe).
  3. Execute Rar.exe passing the a (Add) command, the target archive name, and the source files.

Setup: Ensure Rar.exe is Accessible

If you use this command frequently across scripts, add C:\Program Files\WinRAR to your system's PATH environment variable. This allows you to simply type rar instead of the full path. If not, the script must define the absolute path.

Method 1: Basic RAR Compression

This method creates a standard .rar file of a specific directory.

@echo off
setlocal

:: Define the path to the RAR executable
set "rarExe=C:\Program Files\WinRAR\Rar.exe"

:: Define Source and Target
set "sourceDir=C:\Project\SourceCode"
set "archiveFile=C:\Backups\Project_Backup.rar"

:: Check if Rar.exe exists
if not exist "%rarExe%" (
echo [ERROR] WinRAR not found at "%rarExe%".
pause
exit /b 1
)

:: Check if the source directory exists
if not exist "%sourceDir%\" (
echo [ERROR] Source directory "%sourceDir%" does not exist.
pause
exit /b 1
)

echo Beginning compression of "%sourceDir%"...
echo.

:: Execute Rar compression
:: 'a' means Add to archive
:: '-ep1' excludes the base directory name from the paths (only puts the contents inside the archive)
:: '-m5' sets the compression method to Maximum (slowest, best compression)
:: '-y' assumes Yes on all queries (prevents overwrite prompts from hanging the script)
"%rarExe%" a -ep1 -m5 -y "%archiveFile%" "%sourceDir%\*"

if %errorlevel% equ 0 (
echo.
echo ==========================================
echo COMPRESSION SUCCESSFUL
echo Saved to: %archiveFile%
echo ==========================================
) else (
echo.
echo [ERROR] WinRAR returned error code: %errorlevel%.
pause
exit /b %errorlevel%
)

pause
endlocal
tip

The -ep1 flag strips the base directory from the stored paths. Without it, extracting the archive recreates the full C:\Project\SourceCode\ hierarchy. With it, only the contents appear at the archive root.

Method 2: Creating Encrypted and Solid Archives

For highly secure data or archives consisting of thousands of tiny text files, the RAR format shines. You can create a "Solid" archive (which treats all files as a single data stream for better compression) and encrypt it with AES-256.

@echo off
setlocal

set "rarExe=C:\Program Files\WinRAR\Rar.exe"
set "sourceData=C:\Finance\QuarterlyData\*.*"
set "archiveFile=C:\Vault\SecureData.rar"

:: Check if Rar.exe exists
if not exist "%rarExe%" (
echo [ERROR] WinRAR not found at "%rarExe%".
pause
exit /b 1
)

:: Prompt the user for a password so it is not stored in plain text
set /p "password=Enter archive password: "
if "%password%"=="" (
echo [ERROR] Password cannot be empty.
pause
exit /b 1
)

:: Ensure the output directory exists
for %%A in ("%archiveFile%") do (
if not exist "%%~dpA" mkdir "%%~dpA"
)

echo Compressing and encrypting data...
echo.

:: 'a' = Add
:: '-s' = Create solid archive
:: '-hp' = Encrypt both file data AND headers (hides the filenames inside)
:: '-y' = Assume Yes on all queries
"%rarExe%" a -s -hp"%password%" -y "%archiveFile%" "%sourceData%"

if %errorlevel% equ 0 (
echo.
echo ==========================================
echo ENCRYPTION AND COMPRESSION SUCCESSFUL
echo Saved to: %archiveFile%
echo ==========================================
) else (
echo.
echo [ERROR] WinRAR returned error code: %errorlevel%.
pause
exit /b %errorlevel%
)

pause
endlocal
warning

Never hard-code passwords directly in a Batch script. The example above uses set /p to prompt the operator at runtime. For fully unattended scenarios, read the password from a permissions-restricted file or a secrets manager instead.

Why Create RAR Archives in Batch?

  1. Scheduled Backups: Integrating WinRAR into a nightly scheduled task to compress web application logs before moving them to cold storage.
  2. Recovery Volumes: WinRAR uniquely supports a -rv flag, allowing you to generate Recovery Volumes. This is useful for placing critical backup archives on unstable network drives where data corruption might occur.
  3. Splitting Large Files: If an archive exceeds an email limit or a USB drive size (FAT32 limit of 4 GB), RAR supports splitting files using the -v flag (e.g., -v2000m splits into 2 GB chunks like .part1.rar, .part2.rar).

Important Considerations

  1. Licensing: While 7-Zip is open source and entirely free for commercial use, WinRAR is a commercial, licensed product. Ensure your organization permits the use of WinRAR in automated production workflows.
  2. File Overwrite: By default, executing Rar.exe against an existing archive updates it (adds new files, modifies changed ones). If you want an entirely fresh archive, use del "%archiveFile%" 2>nul before running the RAR command, or append a timestamp suffix to the filename.
  3. File Locking: RAR cannot compress files that are currently open or locked by other programs (like active SQL databases). You must stop the service or create a shadow copy before archiving.

Conclusion

Integrating Rar.exe into your Batch scripts unlocks powerful archiving capabilities. By automating maximum compression rates, solid data streams, and encrypted file headers, you ensure that critical corporate data backups remain highly reduced in size and protected from unauthorized access.