Recursively unzip files where they reside, then delete the archives

17,538

Solution 1

A quick and dirty powershell script to do what you need, you'll need 7zip commandline version. Just change the two paths in the sript and TEST it first, don't have the opportunity to do so myself at the moment.

$folderPath="X:\Test";

Get-ChildItem $folderPath -recurse | %{ 

    if($_.Name -match "^*.`.zip$")
    {
        $parent="$(Split-Path $_.FullName -Parent)";    
        write-host "Extracting $($_.FullName) to $parent"

        $arguments=@("e", "`"$($_.FullName)`"", "-o`"$($parent)`"");
        $ex = start-process -FilePath "`"C:\Path\To\7zip\7z.exe`"" -ArgumentList $arguments -wait -PassThru;

        if( $ex.ExitCode -eq 0)
        {
            write-host "Extraction successful, deleting $($_.FullName)"
            rmdir -Path $_.FullName -Force
        }
    }
}

Solution 2

This can be run on the command line:

for /r %f in (*.zip) do 7z x "%f" -o"%~pf" && del "%f"

Details of for /r: https://ss64.com/nt/for_r.html

The %~pf is expanded to the path (as stated in 'for' command help: for /?).

Share:
17,538
Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin over 1 year

    I goofed up a setting on the backup software I use. Normally I would have the files from a network share get loaded into a spare drive, compressed into zip folders, then sent off to the offsite hub.

    What instead happened was the compression happened first, so now I have a network share full of my original directories, but all the files have been compressed into their own zip folder.

    Is there a fast way I could decompress all zip folders on the server where they are, then delete the ZIP files? I have 7-zip which seems like it might do the job.

    What I've tried so far:

    I've ran a search for and ZIP files, then selected "Extract Here" from the 7zip menu, but that extracts the ZIP files to whatever folder I happened to have right clicked on, instead of where they actually reside. I have file versioning turned on, but the latest backup I have is too far in the past.

  • gm2
    gm2 almost 11 years
    I tested this powershell script and it works for me. What's the specific text of the error message?
  • Admin
    Admin almost 11 years
    I get error "set folderPath="C:/test" do was unexpected at this time."
  • gm2
    gm2 almost 11 years
    Please try it again. I've fixed an issue in the command where the backslash immediately following each instance of %folderPath% wasn't being rendered in the code block. I forgot that backslash is an escape character in markdown syntax.
  • Admin
    Admin almost 11 years
    "x folderPathf was unexpected at this time"
  • gm2
    gm2 almost 11 years
    Are you running this in a batch file? If so you need to replace all %f with %%f. Don't change %folderPath%
  • gm2
    gm2 almost 11 years
    WinRAR's Extract each archive to separate folder produces same output as 7-Zip's Extract to "\*" when multiple zip files are selected. Does not decompress all zip files in-place as @moses wants.
  • Admin
    Admin almost 11 years
    I don't want each archive to be extracted to its own folder. Like I said in the question, I want them extracted into the folders where they currently reside.
  • Admin
    Admin over 10 years
    This ended up working for me, sorry about the lateness. Is there any way I could instruct 7zip in this script to ignore a particular .zip filename in the group?
  • Martin
    Martin over 10 years
    Yeah, simply change the first if-condition to: if(($_.Name -match "^*.`.zip$") -and ($_.Name -notmatch "nameToIgnore"))
  • eminsenay
    eminsenay over 10 years
    using "x" instead of "e" under the arguments extracts preserving the directory structure of the zip file.
  • autobottodoggo
    autobottodoggo about 10 years
    @gm2: this has not been my experience with 7-zip. What you described is exactly what you'd expect it to do, but it always dumps everything into a folder named [0]~' for me. Irritating.
  • Eric Cosky
    Eric Cosky over 9 years
    You can add "-NoNewWindow" to the start-process to prevent a new console window from popping up with each 7z command.
  • Zulgrib
    Zulgrib about 3 years
    What is the purpose of ` character when declaring 7zip's path ?
  • Martin
    Martin about 3 years
    @Zulgrib the ` (backtick) is an escape character, and is used to the escape the .(dot) so that it is treated as a literal dot, and not as a special regex character. You can also see that it is used to escape double quotes (") when they appear in the middle of a string.
  • masroore
    masroore over 2 years
    Thanks! Worked perfectly.