问题
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 WinRAR 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:
- source folder
- subfolder1
- Archive1.r00
- Archive1.r01
- Archive1.r02
- xxx.txt
- subfolder2
- Archive2.r00
- Archive2.r01
- yyy.txt
- subfolder3
- Archive3.r00
- Archive3.r01
- Archive3.r02
- Archive3.r04
- Archive3.r05
- zzz.txt
- subfolder1
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.
How to unpack all rar archives in all subfolders of a folder and then delete the archives?
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.
Edit.1
If winrar is ready with the first subfolder the structure in the source folder should look like this:
- source folder
- subfolder2
- Archive2.r00
- Archive2.r01
- yyy.txt
- subfolder3
- Archive3.r00
- Archive3.r01
- Archive3.r02
- Archive3.r04
- Archive3.r05
- zzz.txt
- subfolder2
The files and folders in C:\Users\new-location
should look like this:
- source folder
- subfolder1
- xxx.mp4
- xxx.txt
- subfolder2
- yyy.mp4
- yyy.txt
- subfolder3
- zzz.mp4
- zzz.txt
- subfolder1
回答1:
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 for 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 WinRAR version 5.21. RAR 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 errorlevel is lower than 1, there was no error and the 3 commands of the if 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 rd 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 for loop could be used to remove the empty base source folder as well, but keep the folder if anything failed.
来源:https://stackoverflow.com/questions/31464037/how-to-extract-all-multi-volume-rar-archives-from-subfolders-of-a-folder