I have some splitted archives in C:\test
this way
PARENT folder is C:\test
|
--- subfolder1
|
-- file1.part01.rar
-- file1.part02.rar
-- file2.part01.rar
-- file2.part02.rar
.....
--- subfolder2
|
-- file1.part1.rar
-- file1.part2.rar
-- file2.part1.rar
-- file2.part2.rar
......
I try to extract these folders in this way
PARENT folder is C:\test
|
--- subfolder1
|
-- file1 [folder]
-- file2 [folder]
.....
--- subfolder2
|
-- file1 [folder]
-- file2 [folder]
......
but I fails (batch seems don't start)
@echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "tokens=2 delims=:" %%I in ('%SystemRoot%\System32\chcp.com') do set "CodePage=%%I"
%SystemRoot%\System32\chcp.com 65001 2>nul
set "PromptForBreak="
if /I "%~1" == "/noprompt" set "PromptForBreak=rem"
set "SourceFolder=C:\Test"
set "LogExtract=%SourceFolder%\ExtractionLog.txt"
set "LogError=%SourceFolder%\ErrorLog.txt"
del /Q "%LogExtract%" "%LogError%" 2>nul
for /F "eol=| delims=" %%I in ('dir "%SourceFolder%\*" /AD-H /B /ON 2^>nul ^| %SystemRoot%\System32\findstr.exe /I /L /V /X /C:done') do (
set "NoFolderMove="
for /F "eol=| delims=" %%J in ('dir "%SourceFolder%\%%I\*.rar" "%SourceFolder%\%%I\*.zip" /A-D-H /B /ON 2^>nul') do (
if exist "%SourceFolder%\%%I\%%J" (
echo Extracting "%SourceFolder%\%%I\%%J" ...
"%ProgramFiles%\WinRAR\WinRAR.exe" x -cfg- -ibck -logpfu="%LogExtract%" -o -y -- "%SourceFolder%\%%I\%%J" "%SourceFolder%\%%I\"
if errorlevel 1 (
set "NoFolderMove=1"
set "ArchiveFile=%SourceFolder%\%%I\%%J"
>>"%LogError%" call echo Error %%ErrorLevel%% on extracting "%%ArchiveFile%%"
) else (
echo %%~nJ| %SystemRoot%\System32\findstr.exe /I /R "\.part[0123456789][0123456789]*$" >nul
if errorlevel 1 ( del /F "%SourceFolder%\%%I\%%J" ) else for %%# in ("%%~nJ") do del /F /Q "%SourceFolder%\%%I\%%~n#.part*%%~xJ"
)
)
)
if not defined NoFolderMove (
md "%SourceFolder%\done" 2>nul
if exist "%SourceFolder%\done\" move /Y "%SourceFolder%\%%I" "%SourceFolder%\done\" >nul
%PromptForBreak% %SystemRoot%\System32\choice.exe /C NY /N /T 2 /D N /M "Break execution [N/Y]? "
%PromptForBreak% if errorlevel 2 goto EndBatch
)
)
:EndBatch
if defined CodePage %SystemRoot%\System32\chcp.com %CodePage%
endlocal
CodePudding user response:
The batch file code in the question runs without any error. It does not produce the wanted directory tree on using the four multi-volume RAR archives created by myself, but there is no error output during the batch file execution.
The following batch file can be used for this task on using WinRAR respectively Rar.exe
version 5.70 or a newer version supporting the switches -ad1
(required) and -isnd-
(not necessary):
@echo off
setlocal EnableExtensions DisableDelayedExpansion
"%ProgramFiles%\WinRAR\Rar.exe" x -ad1 -cfg- -c- -idq -isnd- -o -r -y -- "C:\Test\*.rar"
if errorlevel 1 pause & exit /B
for /F "delims=" %%I in ('dir "C:\Test\*.part*" /AD /B /S 2^>nul') do (
if exist "%%I\%%~nI\" (
dir "%%I" /A /B | %SystemRoot%\System32\findstr.exe /I /L /V /X /C:"%%~nI" | %SystemRoot%\System32\findstr.exe /R "^." >nul
if errorlevel 1 (
move "%%I\%%~nI" "%%~dpI" >nul
if not errorlevel 1 (rd "%%I") else ren "%%I" "%%~nI"
) else ren "%%I" "%%~nI"
) else ren "%%I" "%%~nI"
)
del /A-R /Q /S "C:\Test\*.rar" >nul
endlocal
The RAR command x
and the RAR switches are described all in the manual of the console version of WinRAR which is the text file Rar.txt
in program files directory of WinRAR. The manual contains at bottom also the possible exit values and their meanings.
There can be used also UnRAR.exe
version 5.70 or newer for extracting the RAR archives which is freeware in comparison to Rar.exe
which is shareware. UnRAR.exe
(UnRAR for Windows) can be downloaded from WinRAR and RAR archiver addons.
The batch file executes on any error during the extraction of all archives found recursively by Rar.exe
the command PAUSE to halt the batch file execution to see the error messages and then exits the batch file processing without doing the cleanup steps.
The directory C:\Test
could have following folders and files after the extraction finished:
- subfolder1
- file1.part01
- one or more extracted folders
- one or more extracted files
- file2.part1
- file2
- one or more extracted folders
- one or more extracted files
- file2
- file1.part01.rar
- file1.part02.rar
- file2.part1.rar
- file2.part2.rar
- file1.part01
- subfolder2
- file3.part01
- one or more extracted folders
- one or more extracted files
- file4.part1
- file4
- one or more extracted folders
- one or more extracted files
- one or more additional extracted folders
- one or more additional extracted files
- file4
- file3.part01.rar
- file3.part02.rar
- file4.part1.rar
- file4.part2.rar
- file3.part01
There are not wanted the directory names ending with .part01
or .part1
. So the directories of which name are matched by the wildcard pattern *.part*
are renamed by removing the "extension" .part*
.
There could be also multi-volume RAR archives which contain just a directory with same name as the RAR archive file which contains all the archived directories and files. This is the case in the example above for file2.part?
containing the directory file2
with all the archived directories and files. For such archives it would be better to have finally just file2
and not finally file2\file2
in the directory of the multi-volume RAR archive file. For that reason file2.part1\file2
is moved up to directory subfolder1
with name file2
and the now empty directory file2.part1
is deleted by the batch code.
It is also possible that an archive contains a directory with same name as the archive file, but additionally also other directories and files as it is the case for file4.part?
. In this case no directory move should be done because of the result would be not the same as inside the archive.
Finally the batch file deletes all RAR archive files with exception of those with read-only attribute set because of using option /A-R
. That makes it possible to run the batch file multiple times on same directory structure on all *.rar files having the read-only attribute set and the directories created by Rar.exe
are deleted between each execution of the batch file.
The last but one command line could be also:
del /A /F /Q /S "C:\Test\*.rar" >nul
That command line would delete really all RAR archive files in C:\Test
and all its subdirectories.
The final result of this batch file in C:\Test
is:
- subfolder1
- file1
- one or more extracted folders
- one or more extracted files
- file2
- one or more extracted folders
- one or more extracted files
- file1
- subfolder2
- file3
- one or more extracted folders
- one or more extracted files
- file4
- file4
- one or more extracted folders
- one or more extracted files
- one or more additional extracted folders
- one or more additional extracted files
- file4
- file3
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
del /?
dir /?
echo /?
endlocal /?
exit /?
findstr /?
for /?
if /?
move /?
pause /?
rd /?
ren /?
setlocal /?
Read the Microsoft documentation about Using command redirection operators for an explanation of 2>nul
. The redirection operator >
must be escaped with caret character ^
on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir
command line in a separate command process started in background.