Search code examples
batch-filevbscriptwinrar

Winrar Batch Script (With a twist) - Rename extracted file to current directory


I am looking for a simple batch or vbs script I can edit to complete the following tasks.

  1. Search a folder (including sub dirs) for *.rar files.
  2. Extract the *.rar found to a specific drive i.e E:/ or F:/ (I can change the file for this)
  3. The twist, is the script must rename the extracted file to the directory name.

i.e

C:\Documents\Shop1_A\file.rar

Inside file.rar there is a file.pdf

I require the script to extracted the file.rar to a drive and rename the extracted file to E:\Shop1_A.pdf

There will only ever be 1 file in the archive (no duplicate or overwrite errors)


Solution

  • do you only have pdf files? If not, try this:

    @ECHO OFF &SETLOCAL
    set "SourceFolder=%userprofile%"
    set "DestinationFolder=%temp%"
    
    for /d /r "%SourceFolder%" %%a in (*) do for %%b in ("%%~fa\*.rar") do for /f "delims=" %%c in ('rar lb "%%~Fb"') do (
        rar e -idq "%%~fb" "%%~c" "%DestinationFolder%"
        ren "%DestinationFolder%\%%~c" "%%~na%%~Xc"
    )
    

    If you don't own rar, you can also work with the free unrar.