3

I want to unpack all files in some subfolders which are in a main folder, delete the xxx.rar files after unpacking and move the folder with the files to another location.

  • Main Folder
    • Sub Folder1 (with .rar files)
    • Sub Folder2 (with .rar files)
    • Sub Folder3 (with .rar files)

This my batch script and works so far.

SET "sourcefolder=C:\Users\Unpack"
FOR /R %sourcefolder% %%X in (*.rar) do (
    pushd "%%~dpX"
    "C:\Program Files\WinRAR\Rar.exe" x -y "%%X" "*.*" && del "*.rar"
    popd
)
for /d /r %sourcefolder% %%x in (*) do move "%%x" "C:\Users\New-Location")

But I want that every subfolder whose files are unpacked immediately moved to the "New-Location" folder and not only after everything has been unpacked in the main folder.

Some ideas what I have to change in the code?

Mofi
  • 46,139
  • 17
  • 80
  • 143
BASF
  • 147
  • 1
  • 3
  • 17

1 Answers1

7

This little batch code hopefully does what you want.

@echo off
set "SourceFolder=C:\Users\Unpack"
set "TargetFolder=C:\Users\New-Location"
if not exist "%TargetFolder%" md "%TargetFolder%"
"%ProgramFiles%\WinRAR\Rar.exe" x -ad -cfg- -idq -r -y "%SourceFolder%\*.rar" "%TargetFolder%"
del /F /Q /S "%SourceFolder%\*.rar">nul
for /D %%D in ("%SourceFolder%\*") do rd "%%D" 2>nul

Console version Rar.exe is more powerful than most users never reading the manual Rar.txt stored in program files folder of WinRAR are aware of.

Unpacking all *.rar files in all subfolders of a source folder can be done directly with Rar.exe as it can be seen because no for loop is used in batch code. Rar.exe supports wildcards on decompressing RAR archive files and switch -r used on command x results in processing all RAR archive files also in all subfolders as the manual explains.

Option -ad meaning append archive name to destination path could be removed from RAR command line if all archives contain a unique folder name, or all archives should be unpacked into same directory with overwriting already existing files from a previous archive unpacked before. Usage of -ad depends on contents of the archive files.

Option -idq means quiet mode, i.e. output only error messages, but no progress information which is faster.

The deletion of all *.rar files after unpacking them is done also without a for loop as command del supports also deletion of all *.rar files in all subfolders of a folder.

Edit:

For deletion of all subfolders in source folder being empty after deleting all RAR files, but keeping the source folder, a for loop is finally necessary as added to code above.

Subfolders not being empty are ignored by command rd because the parameters /S /Q are not used which would delete a subfolder even if not already completely empty.

The error message of rd output to stderr if a subfolder to remove is not empty is redirected to device nul to suppress it.

To delete all subfolders of source folder independent on what those subfolders contain after unpacking all RAR archives, but keep the source folder, the last two lines of batch code above need to be replaced by the following line:

for /D %%D in ("%SourceFolder%\*") do rd /S /Q "%%D" 2>nul

And for deleting the source folder with all its subfolders, the last two lines of batch code above need to be replaced by the following line:

rd /S /Q "%SourceFolder%" 2>nul

Note: A folder can be removed by rd only if it is not the current working directory for any running process on Windows.

Help for each command used in the batch file can be read by opening a command prompt window and run there:

  • del /?
  • for /?
  • if /?
  • md /?
  • rd /?
  • set /?
  • "%ProgramFiles%\WinRAR\Rar.exe" /?
Mofi
  • 46,139
  • 17
  • 80
  • 143
  • I had read the Rar.exe consol help but have not understanding all the switches :) thx for explain `-ad` and `-idq`. I have found that your script copy the files after unpack to the `md "%TargetFolder%"` location but delete the .rar files only after all files in the sub folders in the main folder are unpacked and leaves the emty folders in the "SourceFolder". To delet the emty folders I used this line now. `for /f "usebackq delims=" %%d in ("dir "C:\Users\Unpack\" /ad/b/s | sort /R") do rd "%%d")` How can I delet the sub folder with his files directly after moving to the "New-Location" ? – BASF Jul 15 '15 at 08:59
  • `Rar.exe` extracts the files and folders directly to the specified target folder (or a subfolder with name of archive file on using `-ad` switch). For deletion of the folders in source folder after *RAR* finished, see edited answer above. – Mofi Jul 15 '15 at 12:28
  • thx again for explain. Thing I was not clear enough. I download folder with .r files in it over a FTP monitoring programm and want that winrar goes in the first sub folder in main folder and start unpack at .r00, delete the archiv and move the folder with his unpacked files to a new location. Then the script should start this process again with the next sub folder. – BASF Jul 16 '15 at 14:13
  • I think so too. I will create a new question. – BASF Jul 16 '15 at 19:07
  • The new question for multi-volume archive extraction with a different algorithm is [here](http://stackoverflow.com/questions/31464037/). – Mofi Jul 17 '15 at 05:26