Bricktop
Bricktop

Reputation: 153

batch extract and delete archives, how to delete .part*.rar

I have been using this script to extract and delete archives but it's not handling archives named .part##.rar correctly for some reason. What am I doing wrong?

for /r %%r in (*.zip *.7z *.rar *.ace) do 7z x -y "%%r" >nul && del "%%r" && echo unpacked "%%~nxr"

edit: I decided to build this into a separate script that handles all archives in a single folder %1

:security
cd /d "%~1" || echo no valid directory defined && exit /b
attrib -s -h *.* >nul
dir *.rar *.zip *.7z *.ace >nul 2>nul || exit /b

:extract
for %%r in (*.zip.001 *.7z.001 *.tar.001) do (
    7z x -y "%%r" >nul && recycle -f "%%r" && echo unpacked "%%~nxr"
    if exist "%%r" echo unpack failed, maybe the disk is full? && exit /b
    recycle -f "%%~dpn.???" && echo multipart archives "%%~dpn.???" recycled
)

for %%r in (*.part1.rar *.part01.rar *.part001.rar) do (
    7z x -y "%%r" >nul && recycle -f "%%r" && echo unpacked "%%~nxr"
    if exist "%%r" echo unpack failed, maybe the disk is full? && exit /b
    rem need a way to delete multipart volumes here
)

for %%r in (*.rar *.zip *.7z *.ace *.tar) do (
    7z x -y "%%r" >nul && recycle -f "%%r" && echo unpacked "%%~nxr"
    if exist "%%r" echo unpack failed, maybe the disk is full? && exit /b
    if /i "%%~xr"==".rar" if exist "%%~dpnr.r00" recycle -f "%%~dpnr.r??" && echo multipart archives "%%~dpn.r??" recycled
)

goto security

I need help with deleting the remaining files in the middle stack.

Upvotes: 0

Views: 987

Answers (2)

user1016274
user1016274

Reputation: 4209

For your second code with expanded loops, here is how to delete the partial .rar archives (loop 2):

setlocal enableDelayedExpansion
for %%R in (*.part1.rar *.part01.rar *.part001.rar) do (
   REM notice uppercase R used!
   REM process first archive "name.part01.rar"...like posted  

   REM now delete all partial archives of this name
   REM isolate archive name
   for %%F in ("%%~nR") do set "files=%%~nF"
   if exist "!files!.part*.rar" ECHO del /Q "!files!.part*.rar"
)

As usual please test this and then remove the ECHO statement.
Notes:
1- in your first loop (handling .zip.001 etc.), you need to use %%dpnr instead of %%dpn. Using uppercase loop variables help spotting this kind of error (%R instead of %r).
2- dir *.rar *.zip *.7z *.ace >nul 2>nul || exit /b should be dir *.rar *.zip *.7z *.ace *.tar >nul 2>nul || exit /b to check for tarfiles as well.

Upvotes: 1

user1016274
user1016274

Reputation: 4209

This will attempt to narrow down the exception as much as possible to the pattern used, using a Regular Expression:

for /F %%f in ('dir /b /a-d *.zip *.7z *.rar *.ace ^|findstr /REIV /C:"\.part[0-9][0-9]*\.rar"') do @(7z x -y "%%f" >nul && del "%%f" && echo unpacked "%%~nxf")

Upvotes: 0

Related Questions