How to unrar all RAR files in a folder to a specific target folder?

Alexandr Pato picture Alexandr Pato · Oct 8, 2015 · Viewed 8.6k times · Source

I have a file batch to unrar all RAR files in a folder into a subfolder.

C:\test\sub-folder\file.rar -> C:\test\sub-folder\fileunrar

But when I run it, the file is unpacked to the base folder:

C:\test\sub-folder\file.rar -> C:\test\fileunrar

This is the batch file I'm currently using:

@ECHO OFF
cd C:\test
SET PATH=C:;C:\Program Files\WinRAR;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;%PATH%
FOR /f "delims=" %%d IN ('DIR /B') DO unRaR x -y -r -o- *.rar
EXIT

Why are the RAR archives extracted to base folder?

Answer

Mofi picture Mofi · Oct 8, 2015

There is the text file Rar.txt in program files folder of WinRAR which is the manual for the console version Rar.exe containing also all the commands and switches supported by UnRAR.exe which of course can't create, update or repair RAR archives.

Running in a command prompt window just UnRAR.exe without any parameter results in getting displayed all supported commands and switches with a brief description.

UnRAR, Rar and WinRAR support all unpacking multiple *.rar files in a directory without the need of a batch file with a FOR loop with a single command.

For unpacking a multi-volume archive it is enough to specify on command line the file name of the first RAR archive file. UnRAR, Rar and WinRAR automatically detect that this file is the first file of a multi-volume archive and processes automatically all volumes.

Of course UnRAR, Rar and WinRAR do not process each *.rar file more than once when unpacking all *.rar files of a folder containing multiple separate archives as well as 1 or more multi-volume archives.

For unpacking multiple independent *.rar archives with a single line the switch -ad might be useful to avoid that the contents of all independent archives are merged together into same directory.

For unpacking all *.rar archives in directory C:\Temp to directory C:\Temp\Extracted with merging the files and folders from all archives to this directory and automatically overwrite all existing files use following command:

"%ProgramFiles%\WinRAR\UnRAR.exe" x -c- -cfg- -inul -o+ -y "C:\Temp\*.rar" "C:\Temp\Extracted\"

Extracting all *.rar files in C:\Temp with getting each independent archive extracted into a subdirectory in C:\Temp\Extracted with name of archive file can be achieved with:

"%ProgramFiles%\WinRAR\UnRAR.exe" x -ad -c- -cfg- -inul -o+ -y "C:\Temp\*.rar" "C:\Temp\Extracted\"

The switch -ad makes the difference in output folder.

It does not matter if C:\Temp\Extracted already exists or does not yet exist for unpacking the archives. UnRAR, Rar and WinRAR create on extraction always the entire directory tree to output folder if that is necessary and of course possible (write permissions, drive/share exists and not write protected).

Let us look on an example for making the difference more clear.

There are the 2 RAR archives MyFirstArchive.rar and AnotherArchive.rar in C:\Temp containing following files and folders.

MyFirstArchive.rar

 Attributes      Size    Date   Time   Name
----------- ---------  -------- -----  ----
    .......      6302  11-10-15 15:25  zzz.doc
    .......    940942  26-08-15 08:07  yyy.txt
    .......   1430688  06-10-14 05:49  Folder1\xxx.pdf
    ...D...         0  12-10-15 07:10  Folder1
----------- ---------  -------- -----  ----
              2377932                  4

AnotherArchive.rar

 Attributes      Size    Date   Time   Name
----------- ---------  -------- -----  ----
    .......     28165  08-01-12 17:50  zzz.doc
    .......      3743  29-10-14 07:18  Folder2\index.txt
    .......     27141  29-10-14 07:18  Folder1\index.txt
    .......    940942  26-08-15 08:07  index.txt
    ...D...         0  12-10-15 07:10  Folder1
    ...D...         0  12-10-15 07:12  Folder2
----------- ---------  -------- -----  ----
               999991                  6

The first command results in:

  • C:\Temp\Extracted
    • Folder1
      • index.txt
      • xxx.pdf
    • Folder2
      • index.txt
    • index.txt
    • yyy.txt
    • zzz.doc

zzz.doc in AnotherArchive.rar has overwritten zzz.doc in MyFirstArchive.rar.

The second command with switch -ad results in:

  • C:\Temp\Extracted
    • MyFirstArchive
      • Folder1
        • xxx.pdf
      • yyy.txt
      • zzz.doc
    • AnotherArchive
      • Folder1
        • index.txt
      • Folder2
        • index.txt
      • index.txt
      • zzz.doc

The folder and files of the 2 archives are extracted to separate folders with name of archive file.