Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm using 7zip cmd line version in my app (written in Python on Win 7 box) to extract .tgz files. Although I'm using the -r switch, the .tar and .tgz subdirectories are not extracted. Can someone tell me if I'm overlooking something or give some direction ... thanks! Below is a variant of the command that I've tried thusfar:

C:> 7za e c:\Extracted\name.tgz -oc:\PathFolder *.tar -r
share|improve this question
    
To clarfiy. Do you have archives with other archives in them, or do you have a set of folders that all have .tar and .tgz files in them? –  soandos Feb 19 '12 at 16:44
    
@soandos "archives with other archives in them ..." –  suffa Feb 19 '12 at 16:51

2 Answers 2

up vote 1 down vote accepted

Here is code that should do what you want (note: It preserves all of the directory structures. If you do not want that, change the x to an e. Run this in powershell)

$cont=true  
cd c:\Extracted  
$TarFilesToExtract = get-childItem *.tar -Recurse  
$TgzFilesToExtract = get-childItem *.tgz -Recurse  
foreach($file in $TarfilesToExtract)  
{   
    7z x $file -o C:\Pathfolder    
}  
foreach($file in $TgzFilesToExtract)    
{  
    7z x $file -o C:\Pathfolder
}  
cd c:\Pathfolder  
while($cont -eq "true")  
{  
    $TarFilesToExtract = get-childItem *.tar -Recurse  
    $TgzFilesToExtract = get-childItem *.tgz -Recurse  
    if($TarFilesToExtract.Length -eq 0 -and $TgzFilesToExtract -eq 0)  
    {  
        $cont = "False"  
    }  
    else  
    {  
        foreach($file in $TarfilesToExtract)  
        {  
            7z x $file  
        }  
        foreach($file in $TgzFilesToExtract)  
        {  
            7z x $file  
        }  
    }  
}  

A shorter simpler version:

$cont=true
cd c:\Extracted
$files = get-childItem -include *.tar,*.tgz -Recurse
foreach($file in $TarfilesToExtract)
{
    7z x $file -o C:\Pathfolder
}
cd c:\Pathfolder  
while($cont -eq "true")
{
    $files = get-childItem -include *.tar,*.tgz -Recurse
    if($files.Length -eq 0)
    {
        $cont = "False"
    }
    else  
    {
        foreach($file in $files)
        {
            7z x $file 
        }
    }
}
share|improve this answer
    
This seems like it would work perfectly, but I'm not so sure that I can build powershell in my wxPython app like I can 7zip (open source), that it can be installed (at same time w/ app) on any number of PCs running win xp or higher in my work environment? –  suffa Feb 19 '12 at 18:19
    
@suffa, you could save it as a .ps1 file, and then run it through the command line (same way you are running 7zip) as powershell script1.ps1 –  soandos Feb 19 '12 at 19:52
    
one question, what would be the correct syntax to only extract .log and .txt with 7zip command line? This works for logs: 7za.exe e c:\Extracted -oc:\Extracted *.log -r (I've tried *.log && *.txt ... but didn't work). –  suffa Feb 19 '12 at 20:13
    
@suffa, so I am not really that good with powershell, so I would just do the same thing twice, once for .log and once for .txt –  soandos Feb 19 '12 at 20:16

This worked well for me:

FOR /R "C:\whatever" %I IN (*.gz) DO .7z x "%I" -aou -o"%~dpI"

C:\whatever being the path to look in, *.tgz being the file types to extract and .7z being the location of 7zip (on the PATH in this case).

I also wanted to remove all the compressed files afterwards:

FOR /R "C:\whatever" %I IN (*.gz) DO del "%I"

As mentioned in the thread linked, it would be a good idea to test the command first with an @ECHO:

FOR /R "C:\whatever" %I IN (*.gz) DO @ECHO.7z x "%I" -aou -o"%~dpI"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.