I have these .rar/zip archives into C:\test

test1.rar
test2.rar

Inside test1.rar I haven't folders, just files while in test2.rar I have also a folder inside I try to extract every rar/zip archives into relative folders with archive content into C:\test2.
I want to use same name of archives for relative folder name

test1 <-- in this folder there are only files because test1.rar had not folders within
test2 <-- content should extracted preserving structure folder inside archive
 

Script that I use is this

@echo off
set "SourceFolder=C:\test"
set "TargetFolder=C:\test2"
if not exist "%TargetFolder%" md "%TargetFolder%"
"C:\Program Files (x86)\WinRAR\Rar.exe" x -cfg- -idq -r -y "%SourceFolder%\*.rar" "%TargetFolder%"
del /F /Q /S "%SourceFolder%\*.rar">nul
for /D %%D in ("%SourceFolder%\*") do rd "%%D" 2>nul

but this script works bad

  1. After extraction this script delete my .rar archives from Source Folder, but I don't want this
  2. It doesn't create test1 folder before to move test1.rar content inside test1 folder. It simply extract content

THIS is output that I expected

C:\test2
   |
   |--- test1 [folder]
   |       |
   |       |--a.txt
   |       |--b.txt
   |
   |--- test2 [folder]
          |
          |--simple [folder]
          |     |
          |     |- c.txt
          |
          |-d.txt

But I get this bad output

C:\test2
   |
   |
   |--a.txt
   |--b.txt
   |--simple [folder]
          |     |
          |     |- c.txt
          |
          |-d.txt
1

There are 1 best solutions below

0
Jack On

Solution is this. Thanks to @Mofi for -ad switch

@echo off
set "SourceFolder=C:\temp"
set "TargetFolder=C:\temp2"
if not exist "%TargetFolder%" md "%TargetFolder%"
"C:\Program Files\WinRAR\Rar.exe" x -ad -cfg- -idq -r -y "%SourceFolder%\*.rar" "%TargetFolder%"

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