Long story short I have one folder on a network share, that has 200+ other folders in them, some of which have .rar files in them. I want to extract all the .rar files IN PLACE. Not to another dir. For example I have 200+ folders inside the share z:\Foo. dir names 1-200 for simplicity sake. And I have .rar files in folders 2,5 and 20. (realistically its like 100+ .rar files) I want to run a command from "z:\Foo" that will look through all folders inside foo and extract the .rar files in the folder it finds them in, so 2, 5 and 20.

I have tried navigating to "z:\Foo" and running the command

FOR /F "usebackq" %a in (`DIR /s /b *.rar`) do 7z.exe e %a"

which works great but extracts all files to "z:\Foo" I was looking into using cmd prompt to run a command to search for .rar files and execute that command from within that dir but have had no luck. Something along the lines of

find . -type f -name *.rar -execdir FOR /F "usebackq" %a in (`DIR /s /b *.rar`) do 7z.exe e %a *.rar ';'

But that was a quick copy-pasta from another forum and I havent had the time or brainpower to diagnose it Nor have I had any luck modifying the code to try and get it to extract to the dir it finds the .rar file in. I have tried several other programs as well but none of them seem to work properly. I'm open to other options but would really just like to get this code working for simplicity sake. Ideally I want to just have to run this code every once and a while. Side note, I don't care if it overwrites files already unzipped but would prefer it skip files already unzipped, and I don't want it to delete the .rar file afterwards. Thanks for your time! Another thing, Im doing it this way because its usually 90% .rar files but sometimes .7zip files. Would like to keep it 7zip as the program so I can change the code to work on different zip types.

  • 1
    Have you tried with x (eXtract with full paths) instead of e (Extract without using directory names) (See 7z -?)
    – Stephan
    Mar 25, 2022 at 8:46
  • When the command is ran from z:\foo with x or e it outputs the files from the subfolders into z:\foo hence one of my thoughts was to run a command that searched recursively for .rar files under z:\foo and runs the command in that folder then moves to the next. Didnt find much documentation on getting find to execute code in the dir it finds it in though C:
    – Taeleus
    Mar 25, 2022 at 10:43

1 Answer 1


You could do the following

for /f %i in ('dir /b /s *.rar') do pushd "%~dpi" & 7z x "%i" & popd

With the pushd you change the directory, %~dpi extracts the directory from the variable i, and with the popd you go back again.

  • This workded on my test environment. First you have to pushd yourself to the base folder, then use the command provided by @some guy... Mar 25, 2022 at 12:53
  • THIS worked like a charm. I just made a batch file to move to my Z: drive, and then CD to my Foo dir and then run the command. VIOLA. Worked like charm. Thanks! Now to run it in my BIG actual Foo folder and give it a day or two XD CHEERS!
    – Taeleus
    Mar 27, 2022 at 0:14
  • For those looking to follow this but already have some data extracted and dont want to answer yes/no for every file, I added "-aos" to the code to automatically skip existing files if they were previously extracted. Final code is for /f %i in ('dir /b /s *.rar') do pushd "%~dpi" & 7z x -aos "%i" & popd Massive thanks again to some guy for helping with this!
    – Taeleus
    Apr 3, 2022 at 4:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .