Previously, I was asked to create the following batch.
How can I extract multiple archives to each folder with WinRAR command line?
I thought it was working properly, but when the folder name contained Unicode characters(❤, ㉓ etc), the compressed file in the folder was never decompressed. Therefore, how can I extract Unicode characters even if they are included in the folder name?
As another problem, the following batch does not move to the done folder if there are no compressed files (zip, rar, partX.rar) in the folder. Even if there are no compressed files in the folder, I want to move to the done folder each time.
Before:
C:
│
└─test
├─AAAA
│ XXXX.rar
│ XXXX.jpg
│
├─㉓
│ XXXX.zip
│ XXXX.jpg
│
├─CCCC(error_file)
│ XXXX.rar
│ XXXX.jpg
│
├─DDDD
│ XXXX.part1.rar
│ XXXX.part2.rar
│ XXXX.jpg
│
└─EEEE
XXXX.jpg
After:
C:
│
└─test
├─done
│ │
│ │
│ ├─AAAA
│ │ XXXX.doc
│ │ XXXX.jpg
│ │
│ ├─㉓
│ │ XXXX.doc
│ │ XXXX.jpg
│ │
│ ├─DDDD
│ │ XXXX.doc
│ │ XXXX.jpg
│ │
│ └─EEEE
│ XXXX.jpg
│
└─CCCC(error_file)
XXXX.rar
XXXX.jpg
@echo off
setlocal EnableExtensions DisableDelayedExpansion
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 "ArchiveExtracted="
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 "ArchiveFile=%SourceFolder%\%%I\%%J"
>>"%LogError%" call echo Error %%ErrorLevel%% on extracting "%%ArchiveFile%%"
) else (
set "ArchiveExtracted=1"
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 defined ArchiveExtracted (
md "%SourceFolder%\done" 2>nul
if exist "%SourceFolder%\done\" move /Y "%SourceFolder%\%%I" "%SourceFolder%\done\"
%PromptForBreak% %SystemRoot%\System32\choice.exe /C NY /N /T 2 /D N /M "Break execution [N/Y]? "
%PromptForBreak% if errorlevel 2 goto EndBatch
)
)
:EndBatch
endlocal