0

I would like to extract files having the extension ".wav" from a set of nested .zip files contained in "c:\Myfolder".

This code explains how to extract the .wav files from a .zip. I suppose it's best to first extract all the zip files to a folder before trying the code below.

#requires -Version 5.0

# change $Path to a ZIP file that exists on your system!
$Path = "$Home\Desktop\Test.zip"

# change extension filter to a file extension that exists
# inside your ZIP file
$Filter = '*.zip' # first extract the zip files

# change output path to a folder where you want the extracted
# files to appear
$OutPath = 'C:\ZIPFiles'

# ensure the output folder exists
$exists = Test-Path -Path $OutPath
if ($exists -eq $false)
{
  $null = New-Item -Path $OutPath -ItemType Directory -Force
}

# load ZIP methods
Add-Type -AssemblyName System.IO.Compression.FileSystem

# open ZIP archive for reading
$zip = [System.IO.Compression.ZipFile]::OpenRead($Path)

# find all files in ZIP that match the filter (i.e. file extension)
$zip.Entries | 
  Where-Object { $_.FullName -like $Filter } |
  ForEach-Object { 
    # extract the selected items from the ZIP archive
    # and copy them to the out folder
    $FileName = $_.Name
    [System.IO.Compression.ZipFileExtensions]::ExtractToFile($_, "$OutPath\$FileName", $true)
    }

# close ZIP file
$zip.Dispose()

# open out folder
explorer $OutPath 
val
  • 1,629
  • 1
  • 30
  • 56
  • `$Filter = '*.wav'` -> `$Filter = '*.zip'`? – Ansgar Wiechers Jan 15 '19 at 19:17
  • @AnsgarWiechers: I don't think so, because I want to extract the .wav files inside the zip inside the zip files. If I change .wav to .zip as you suggest, how will the script know to extract the .wav files? – val Jan 15 '19 at 19:20
  • 1
    You first need to extract the nested zip files (e.g. to a temp folder) before you can extract anything from them. – Ansgar Wiechers Jan 15 '19 at 19:22
  • @AnsgarWiechers: OK. Could you show me how? I have a folder with hundreds of zip files, each of which has many zip files that may contains .wav files. – val Jan 15 '19 at 19:25
  • The same way you'd extract a .wav file, obviously. The only difference is the extension you're looking for, and probably the location you're extracting the file to. – Ansgar Wiechers Jan 15 '19 at 19:26
  • @AnsgarWiechers: why can't it be done recursively? – val Jan 15 '19 at 19:33
  • Where did I say it can't? That has nothing to do with what I wrote, though. Even when using recursion you have to a) extract the nested zip files, and b) extract the .wav files from those extracted zip files. In that order. – Ansgar Wiechers Jan 15 '19 at 20:45
  • If you were asking why you can't extract files from nested zip files without extracting them first: zip archive extracting simply doesn't work that way, regardless how much you want it to. – Ansgar Wiechers Jan 15 '19 at 23:43

0 Answers0