How to unpack all rar archives in all subfolders of a folder and then delete the archives?

BASF picture BASF · Jul 15, 2015 · Viewed 9.4k times · Source

I want to unpack all files in some subfolders which are in a main folder, delete the xxx.rar files after unpacking and move the folder with the files to another location.

  • Main Folder
    • Sub Folder1 (with .rar files)
    • Sub Folder2 (with .rar files)
    • Sub Folder3 (with .rar files)

This my batch script and works so far.

SET "sourcefolder=C:\Users\Unpack"
FOR /R %sourcefolder% %%X in (*.rar) do (
    pushd "%%~dpX"
    "C:\Program Files\WinRAR\Rar.exe" x -y "%%X" "*.*" && del "*.rar"
    popd
)
for /d /r %sourcefolder% %%x in (*) do move "%%x" "C:\Users\New-Location")

But I want that every subfolder whose files are unpacked immediately moved to the "New-Location" folder and not only after everything has been unpacked in the main folder.

Some ideas what I have to change in the code?

Answer

Mofi picture Mofi · Jul 15, 2015

This little batch code hopefully does what you want.

@echo off
set "SourceFolder=C:\Users\Unpack"
set "TargetFolder=C:\Users\New-Location"
if not exist "%TargetFolder%" md "%TargetFolder%"
"%ProgramFiles%\WinRAR\Rar.exe" x -ad -cfg- -idq -r -y "%SourceFolder%\*.rar" "%TargetFolder%"
del /F /Q /S "%SourceFolder%\*.rar">nul
for /D %%D in ("%SourceFolder%\*") do rd "%%D" 2>nul

Console version Rar.exe is more powerful than most users never reading the manual Rar.txt stored in program files folder of WinRAR are aware of.

Unpacking all *.rar files in all subfolders of a source folder can be done directly with Rar.exe as it can be seen because no for loop is used in batch code. Rar.exe supports wildcards on decompressing RAR archive files and switch -r used on command x results in processing all RAR archive files also in all subfolders as the manual explains.

Option -ad meaning append archive name to destination path could be removed from RAR command line if all archives contain a unique folder name, or all archives should be unpacked into same directory with overwriting already existing files from a previous archive unpacked before. Usage of -ad depends on contents of the archive files.

Option -idq means quiet mode, i.e. output only error messages, but no progress information which is faster.

The deletion of all *.rar files after unpacking them is done also without a for loop as command del supports also deletion of all *.rar files in all subfolders of a folder.

Edit:

For deletion of all subfolders in source folder being empty after deleting all RAR files, but keeping the source folder, a for loop is finally necessary as added to code above.

Subfolders not being empty are ignored by command rd because the parameters /S /Q are not used which would delete a subfolder even if not already completely empty.

The error message of rd output to stderr if a subfolder to remove is not empty is redirected to device nul to suppress it.

To delete all subfolders of source folder independent on what those subfolders contain after unpacking all RAR archives, but keep the source folder, the last two lines of batch code above need to be replaced by the following line:

for /D %%D in ("%SourceFolder%\*") do rd /S /Q "%%D" 2>nul

And for deleting the source folder with all its subfolders, the last two lines of batch code above need to be replaced by the following line:

rd /S /Q "%SourceFolder%" 2>nul

Note: A folder can be removed by rd only if it is not the current working directory for any running process on Windows.

Help for each command used in the batch file can be read by opening a command prompt window and run there:

  • del /?
  • for /?
  • if /?
  • md /?
  • rd /?
  • set /?
  • "%ProgramFiles%\WinRAR\Rar.exe" /?