How to recursively extract all .tar and .tgz file zipped in a .tgz file using 7zip command line version

8,548

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 -oC:\Pathfolder    
}  
foreach($file in $TgzFilesToExtract)    
{  
    7z x $file -oC:\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 -oC:\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:
8,548

Related videos on Youtube

suffa
Author by

suffa

Although I learned C++ and Java in college, I love writing code in Python and working in the Linux/Unix shell environment. I'm not sure about the market demands, but I plan to hone my craft in Python and Linux/Unix shell scripting (Admin type stuff) ... I feel this is my niche.

Updated on September 18, 2022

Comments

  • suffa
    suffa over 1 year

    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
    
    • soandos
      soandos about 12 years
      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?
  • suffa
    suffa about 12 years
    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?
  • soandos
    soandos about 12 years
    @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
  • suffa
    suffa about 12 years
    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).
  • soandos
    soandos about 12 years
    @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