Question

I have a bunch of rar files some of which contain just the file or files and some have A directory structure

i'd like to create a bat file that can extract the rar with the directory as is & if no directory use the rar file name to create a directory & then extract to that off course handling any errors

so this cmd will output a list to a text file

C:\Program Files\WinRAR>UnRAR.exe l H:\temp\test.rar >H:\temp\test.txt

results in

UNRAR 4.20 freeware      Copyright (c) 1993-2012 Alexander Roshal

Archive H:\temp\Test.rar

 Name             Size   Packed Ratio  Date   Time     Attr      CRC   Meth Ver
-------------------------------------------------------------------------------
 Test.TXT            0        0   0% 20-11-12 18:44  .....A.   00000000 m0b 2.9
-------------------------------------------------------------------------------
    1                0        0   0%

for a rar file without a directory structure and

UNRAR 4.20 freeware      Copyright (c) 1993-2012 Alexander Roshal

Archive H:\temp\testDir.rar

 Name             Size   Packed Ratio  Date   Time     Attr      CRC   Meth Ver
-------------------------------------------------------------------------------
 Test.TXT            0        0   0% 20-11-12 18:44  .....A.   00000000 m0b 2.9
 test                0        0   0% 20-11-12 18:45  .D.....   00000000 m0  2.0
-------------------------------------------------------------------------------
    2                0        0   0%

with a directory

I could create a perl script that will output this listing to a temp text file read it find / pattern match .D..... test if that directory exists & test if the files exits

then create another bath file to extract the files

But I was wondering if there was a simpler way?

Thanx

Was it helpful?

Solution

You can start with a batch scrpit like this:

@echo off
setlocal EnableDelayedExpansion

for %%a in (*.rar) do (
  UnRAR.exe l "%%a" | findstr /C:".D....." >nul
  if !errorlevel!==0 (
    echo File %%a contains dirs
    UnRAR.exe x "%%a"
  )
  if !errorlevel!==1 (
    echo File %%a does not contain dirs, extracting in %%~na
    mkdir "%%~na"
    UnRAR.exe x "%%a" "%%~na\"
  )
)

This will execute UnRAR.exe l filename for every *.rar file in current dir, it then checks if it contains the string .D....., and it extract the rar in the current dir if the string is not found, otherwise it will create a directory with the same filename as the archive (but without the extension) and extracts the archive there. Please check if the syntax of UnRAR.exe that i've used is correct.

EDIT: this codes loops recursively through subdirectories:

@echo off
setlocal EnableDelayedExpansion

for /r "%1" %%a in (*.rar) do (
  UnRAR.exe l "%%a" | findstr /C:".D....." >nul
  if !errorlevel!==0 (
    echo File %%a contains dirs, extracting in "%%~dpa"
    UnRAR.exe x "%%a" "%%~dpa"
  )
  if !errorlevel!==1 (
    echo File %%a does not contain dirs, extracting in %%~dpna
    mkdir "%%~na"
    UnRAR.exe x "%%a" "%%~dpna\"
  )
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top