50262

How to extract all multi-volume RAR archives from subfolders of a folder?

Question:

I search for a way to unpack multi-volume archives after download via batch.

I download folders with .r?? files in it over a FTP monitoring program and want that <em>WinRAR</em> goes in the first subfolder in the source folder and start unpacking .r00, delete the archive and move the folder with the unpacked files to a new location.

Then the batch script should start this process again with the next subfolder.

Let's say the source folder C:\Users\unpack contains following subfolders with files:

<ul><li><strong>source folder</strong> <ul><li><strong>subfolder1</strong> <ul><li>Archive1.r00</li> <li>Archive1.r01</li> <li>Archive1.r02</li> <li>xxx.txt</li> </ul></li> <li><strong>subfolder2</strong> <ul><li>Archive2.r00</li> <li>Archive2.r01</li> <li>yyy.txt</li> </ul></li> <li><strong>subfolder3</strong> <ul><li>Archive3.r00</li> <li>Archive3.r01</li> <li>Archive3.r02</li> <li>Archive3.r04</li> <li>Archive3.r05</li> <li>zzz.txt</li> </ul></li> </ul></li> </ul>

I had start to do this with the script in the link below, but that script can't do what I want, so I have started a new question.

<a href="https://stackoverflow.com/questions/31419391/how-to-unpack-all-rar-archives-in-all-subfolders-of-a-folder-and-then-delete-the" rel="nofollow">How to unpack all rar archives in all subfolders of a folder and then delete the archives?</a>

The script in the link above unrars all files in all subfolders and then moves the folder with its files to a new location. I want that the script unrars and moves subfolder for subfolder in the source folder.

<strong>Edit.1</strong>

If winrar is ready with the first subfolder the structure in the source folder should look like this:

<ul><li><strong>source folder</strong> <ul><li><strong>subfolder2</strong> <ul><li>Archive2.r00</li> <li>Archive2.r01</li> <li>yyy.txt</li> </ul></li> <li><strong>subfolder3</strong> <ul><li>Archive3.r00</li> <li>Archive3.r01</li> <li>Archive3.r02</li> <li>Archive3.r04</li> <li>Archive3.r05</li> <li>zzz.txt</li> </ul></li> </ul></li> </ul>

The files and folders in C:\Users\new-location should look like this:

<ul><li><strong>source folder</strong> <ul><li><strong>subfolder1</strong> <ul><li>xxx.mp4</li> <li>xxx.txt</li> </ul></li> <li><strong>subfolder2</strong> <ul><li>yyy.mp4</li> <li>yyy.txt</li> </ul></li> <li><strong>subfolder3</strong> <ul><li>zzz.mp4</li> <li>zzz.txt</li> </ul></li> </ul></li> </ul>

Answer1:

A possible batch code for this task is:

@echo off setlocal EnableDelayedExpansion set "BaseSourceFolder=C:\Users\Unpack" set "BaseTargetFolder=C:\Users\New-Location" for /D %%D in ("%BaseSourceFolder%\*") do ( set "TargetFolder=%BaseTargetFolder%\%%~nxD" if not exist "!TargetFolder!" md "!TargetFolder!" "%ProgramFiles%\WinRAR\Rar.exe" x -cfg- -idq -y "%%~fD\*.r??" "!TargetFolder!" if not errorlevel 1 ( del /F /Q "%%~fD\*.r??" move /Y "%%~fD\*" "!TargetFolder!">nul 2>nul rd "%%~fD" 2>nul ) ) rem rd "%BaseSourceFolder%" 2>nul endlocal

for /? executed in a command prompt window displays help for command <strong>for</strong> with parameter /D which means for each directory matched by * in base source folder.

In the loop first the target folder name is defined based on name of the subfolder to process. %%~fD and %%~nxD are also explained by for /? whereby folders usually do not have an extension and therefore %%~nD is often also enough.

Next this target folder is created if not already existing.

Then Rar.exe is executed to extract the multi-volume archive in the current subfolder directly to the defined target folder.

*.r?? is used to make this batch file work for multi-volume archives with old naming scheme ArchiveName.r00, ArchiveName.r01, ... as well as better naming scheme ArchiveName.part01.rar, ArchiveName.part02.rar, ... which is used by default by <em>WinRAR</em> version 5.21. <em>RAR</em> automatically skips the archive files processed already during extraction of a multi-volume archive from the list matching *.r??.

Exit code of Rar.exe is evaluated to determine if any error occurred. If exit code assigned to <strong>errorlevel</strong> is lower than 1, there was no error and the 3 commands of the <strong>if</strong> branch are executed resulting in deleting first all RAR archive files.

The remaining files in current subfolder are also moved to the current target folder which is the *.txt file in the folder structure example.

As the current subfolder should be empty now, the command <strong>rd</strong> should be able to remove the directory. In case of an error because subfolder is still not empty, the subfolder remains in base source folder.

The base source folder is empty if everything worked without an error. The commented line after <strong>for</strong> loop could be used to remove the empty base source folder as well, but keep the folder if anything failed.

Recommend

  • Compress several subfolders with batch file (win7)
  • when opened with winrar, Zip file obtained through Cakephp MediaViews reports Unexpected end of arch
  • Installation Error: “This add-on cannot be installed because it appears to be corrupted” [closed]
  • How to Circumvent Perl's string escaping the replacement string in s///?
  • How to calculate CRC of a WinRAR file?
  • unescaped newline inside substitute pattern
  • How to extract distinct part of a string from a file in linux
  • Obtain the `Local Project Path` directory in the project's `Version Control Settings`
  • Error: Program “make” not found in PATH
  • How do I get sikuli to wait until the mouse pointer changes from “busy” to “not busy?”
  • Radio button show hover between two radio button using jquery
  • How to call MoveDataTableCursor in a web test plugin to go through a limited set of rows in a table?
  • In a MEAN stack, how can I do one-time MongoDB indexing?
  • Indexing datetime in MySQL
  • Sticky Footer W/JQuery Mobile
  • R - Change list of ggplot objects into a list of grobs that grid.arrange will accept?
  • How to Compose OSGi Based project with C++ based project?
  • Database First with Bridge Table, How to Keep It Out of Model
  • Change URL of the windows for every different Tab
  • Firefox extension testing and developing - I'm confused
  • How to work with AMMediaType for video filters
  • Counting Treaps
  • Spring boot mapping static html
  • Relative paths. baseUrl and paths not working on ionic2 - angular2
  • Configure nginx to return different files to different authenticated users with the same URI
  • How to use Windows Media Foundation with UWP without a topology
  • Sensibility of combined Maven/Ant+Ivy build management for dual platform Desktop/Android deployment?
  • Getting the scrolling offset when storing coordinates
  • Bootstrap (v3.3.4) glyphicons not displayed in IE when refresh page (F5)
  • Pycharm: Marking a folder as 'sources root' is not recursive for subfolders
  • Uncaught TypeError: $(…).select2 is not a function
  • Button click event not firing in jQuery
  • chrome.tabs.executeScript only fires when the Developer Console is open
  • JSON response opens as a file, but I can't access it with JavaScript
  • Django: Count of Group Elements
  • Fetching methods from BroadcastReceiver to update UI
  • Symfony2: How to get request parameter
  • GridView Sorting works once only
  • WPF Applying a trigger on binding failure
  • Unable to use reactive element in my shiny app