[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 20871
  • Last Modified:

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?
1
bpl5000
Asked:
bpl5000
2 Solutions
 
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now