批量提取和删除档案,如何删除.part * .rar

时间:2017-11-23 09:25:46

标签: batch-file

我一直在使用这个脚本来提取和删除档案,但由于某些原因,它没有正确处理名为.part ##。rar的档案。我做错了什么?

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

编辑:我决定将其构建为一个单独的脚本,用于处理单个文件夹中的所有档案%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

我需要帮助删除中间堆栈中的剩余文件。

2 个答案:

答案 0 :(得分:1)

对于扩展循环的第二个代码,以下是删除部分.rar存档(循环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"
)

像往常一样,请测试一下,然后删除ECHO声明 注:
1-在您的第一个循环中(处理.zip.001等),您需要使用%%dpnr而不是%%dpn。使用大写循环变量有助于发现此类错误(%R而不是%r) 2- dir *.rar *.zip *.7z *.ace >nul 2>nul || exit /b也应dir *.rar *.zip *.7z *.ace *.tar >nul 2>nul || exit /b来检查tarfiles。

答案 1 :(得分:0)

这将尝试使用正则表达式尽可能地将异常缩小到使用的模式:

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")