Using WinRAR at the command line

When I used WinRAR to extract tgz files, I can right click the file and choose "extract here" and it puts it in a folder by the same name as the archive file's name.  For example, H123.tgz would be extracted into a folder named H123.  From the command line, how can I do the same thing?  I can do WinRAR extract *.tgz and it puts the files in the root.  I want to extract more than one file so I don't want to specify the folder name, I just want it to automatically extract it under a folder with the same name as the file.  Can this be done?
LVL 5
bpl5000Asked:
Who is Participating?
 
cepacsCommented:
The command you need is " winrar x *.tgz".  Of course you need to be in the correct directory at the command line.
1
 
bpl5000Author Commented:
That does work, but here's my problem.  I need to recurse into subfolders so I use the -r switch... winrar x -r *.tgz.  Now when I launch this from c:\folder1 and it finds the file abc.tgz in c:\folder1\sub1, it extracts the file to c:\folder1\abc\ instead of c:\folder1\sub1\abc\.  The names of the subfolders will be unknown so I can't specify them.  Any ideas on how I can do this?  By the way, this is being done on a Windows 2003 server.  I'm not stuck on WinRAR... I'm willing to use any program that will unpack a tgz file.
1
 
BarthaxCommented:
Something like the following batch script could be used to recurse the subdirectories (note that it will recurse into extracted content as well).  You will need to call the batch file by its full path in order for the %0 to be populated correctly, i.e., C:\utils\extractall.bat

----------------------
@echo OFF
REM Extract the content of all tgz & tar.gz files in the current directory
for %%f in (*.tgz;*.tar.gz) do "C:\Program Files\WinRAR\Winrar.exe" x "%%f"

REM Perform the same in all sub-directories
for /D %%d in (*) do cd %%d && call %0 && cd ..
----------------------

Let me know if this is suitable & needs tweaking (I haven't tested it as I don't have WinRAR on the machine I'm on...).
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
bpl5000Author Commented:
Thanks cepacs and Barthax.  Barthax, I did something similar to your batch file, but I used VB and instead of doing it recursively, I just used two loops to go into two levels of subdirectories.  This way I won't be going into subdirectories that are created by the extractions.

Thanks for the help!
0
 
steinhajekCommented:
One comment I would like to make is that the script shown above seems to be able to recursively extract winrars, but if a more 'linear' type environment is required it doesn't work.
If I have the following directory structure:
Dir1
Dir2
Dir3
Script won't work; but If I have Dir3 inside of Dir2 inside of Dir1 it will work without any issues.
@echo OFF
REM Extract the content of all tgz & tar.gz files in the current directory
for %%f in (*.zip) do "C:\Program Files\WinRAR\Winrar.exe" x "%%f"

REM Perform the same in all sub-directories
for /D %%d in (*) do cd %%d && call %0 && cd ..

Any way to tweak code to allow for first situation?
Dir1
Dir2
Dir2
etc.
0
 
BarthaxCommented:
I can't explain why that wouldn't work for you.  The "for /D %%d (*)" part means "for each directory (in turn) that match the *" (i.e., all directories without extensions).  Something to note with that command is that it will only (to my knowledge) do the directories which exist at the time the "for /D" part is executed.  The rest of the line uses "call" which means to "put this script on hold & go do ... instead, returning back to here when finished."  If the command did not have the "call", then I would expect what you describe...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.