Skip to content

How to Extract a RAR Archive from a Batch Script

Learn how to automate RAR file extraction in Windows using UnRAR.exe in a Batch script, including handling single/multi-part archives and password-protected files.

Extracting RAR archives programmatically allows your Windows applications to process compressed data automatically upon arrival. Because Windows lacks native decompression support for .rar files, automating the extraction process requires a dedicated command-line utility. Both UnRAR.exe (shipped with WinRAR) and 7z.exe (from 7-Zip) can decompress these formats.

In this guide, we will demonstrate how to automate the extraction of a RAR file directly from a Batch script using the official UnRAR.exe tool.

The Strategy: The UnRAR.exe Command-Line Tool

  1. Ensure WinRAR is installed.
  2. Locate UnRAR.exe (usually in C:\Program Files\WinRAR\UnRAR.exe).
  3. Target your source .rar file and identify an output directory.
  4. Execute the extraction with standard arguments (x for extracting with full paths).

Method 1: Basic UnRAR Extraction

Handling a standard, single-part .rar archive requires the same structural logic used to decompress .zip files.

@echo off
setlocal

:: Define the path to the UnRAR executable
set "unRarExe=C:\Program Files\WinRAR\UnRAR.exe"

:: Define Source and Destination
set "archiveFile=C:\Downloads\ApplicationUpdate.rar"
set "extractDir=C:\App\Temp"

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

:: Check if the archive file exists
if not exist "%archiveFile%" (
    echo [ERROR] Archive not found at "%archiveFile%".
    pause
    exit /b 1
)

:: Make the destination directory if missing
if not exist "%extractDir%\" mkdir "%extractDir%"

echo Beginning extraction of "%archiveFile%"...
echo Destination: "%extractDir%"
echo.

:: Execute UnRAR extraction
:: 'x'  = eXtract with full paths intact
:: '-y' = assume Yes on all queries (prevents overwrite prompts)
:: The trailing backslash on extractDir\ ensures it is interpreted as a folder.
"%unRarExe%" x -y "%archiveFile%" "%extractDir%\"

if %errorlevel% equ 0 (
    echo.
    echo ==========================================
    echo EXTRACTION SUCCESSFUL
    echo ==========================================
) else (
    echo.
    echo [ERROR] UnRAR returned error code: %errorlevel%.
    pause
    exit /b %errorlevel%
)

pause
endlocal

Method 2: Extracting Multi-Part Archives

WinRAR is famous for splitting immense files into segments (e.g., Archive.part1.rar, Archive.part2.rar). When extracting them from a Batch script, you only need to execute the command against the first part. UnRAR.exe will automatically discover and unpack the subsequent parts.

@echo off
setlocal

set "unRarExe=C:\Program Files\WinRAR\UnRAR.exe"
set "multiPartFile=C:\DatabaseDump\DBBackup.part01.rar"
set "outputDir=C:\DatabaseDump\Extracted"

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

:: Check if the first part exists
if not exist "%multiPartFile%" (
    echo [ERROR] Archive not found at "%multiPartFile%".
    pause
    exit /b 1
)

:: Ensure the output directory exists
if not exist "%outputDir%\" mkdir "%outputDir%"

echo Extracting multi-part archive starting from "%multiPartFile%"...
echo.

:: Pass the first chunk: UnRAR finds subsequent parts automatically
"%unRarExe%" x -y "%multiPartFile%" "%outputDir%\"

if %errorlevel% equ 0 (
    echo.
    echo ==========================================
    echo ALL PARTS ASSEMBLED AND EXTRACTED
    echo ==========================================
) else (
    echo.
    echo [ERROR] UnRAR returned error code: %errorlevel%.
    echo The archive may have missing parts or be damaged.
    pause
    exit /b %errorlevel%
)

pause
endlocal

Method 3: Handling Encrypted RAR Files

If an automated pipeline drops a secure, password-protected .rar file into an inbox folder, your Batch script can decrypt it by passing the password string to UnRAR.exe.

@echo off
setlocal

set "unRarExe=C:\Program Files\WinRAR\UnRAR.exe"
set "secureArchive=C:\HR\Payroll_Dec.rar"
set "vaultDir=C:\HR\Secure_Decrypt"

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

:: Check if the archive file exists
if not exist "%secureArchive%" (
    echo [ERROR] Archive not found at "%secureArchive%".
    pause
    exit /b 1
)

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

:: Ensure the output directory exists
if not exist "%vaultDir%\" mkdir "%vaultDir%"

echo Decrypting Vault Archive: "%secureArchive%"
echo.

:: Execute UnRAR
:: '-p' provides the decryption password directly
"%unRarExe%" x -y -p"%archivePass%" "%secureArchive%" "%vaultDir%\"

if %errorlevel% equ 0 (
    echo.
    echo ==========================================
    echo EXTRACTION SUCCESSFUL
    echo ==========================================
) else (
    echo.
    echo [ERROR] UnRAR returned error code: %errorlevel%.
    pause
    exit /b %errorlevel%
)

echo Extraction complete.
pause
endlocal

Why Extract RAR Archives in Batch?

  1. Software Bootstrapping: A setup.bat script that retrieves the newest custom software package delivered securely via .rar, unpacks it locally, and launches an MSI installer quietly.
  2. Legacy Systems Integration: Parsing data delivered from external mainframes that compress their massive tabular data using multi-part .rar formats to bypass email size limitations.
  3. Dedicated CLI Performance: On large file systems, dedicated command-line extraction eliminates the visual overhead of the Windows Explorer context menus or third-party GUI wizards, reducing total deployment time.

Important Considerations

  1. Destination Syntax: WinRAR expects the output directory string to end with a trailing backslash (\). If you pass C:\Output, it might extract the files into the C:\ root and rename the first extracted file "Output". Always use "%extractDir%\".
  2. 7z.exe Alternative: If you lack an enterprise WinRAR license, 7-Zip (7z.exe) can extract nearly all .rar files using similar arguments: 7z x archive.rar -oC:\Output\. However, WinRAR's UnRAR.exe is uniquely capable of performing parity-based repair if the RAR archive is slightly corrupted.
  3. Flat Extraction: Use the e argument instead of x if you wish to strip out all directory structure from the archive and dump all the files flat into a single directory.

Conclusion

Automating reliable RAR decompression guarantees consistent pipeline processing in any Windows environment. Leveraging the precision of UnRAR.exe allows you to bridge password-protected files and multi-segment archives straight into functioning directories without manual intervention.

Navigation