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:
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.
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:
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.
%%~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.
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.r01, ... as well as better naming scheme
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
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.