Retrieve file names from a zip file

JARichard74

Board Regular
Joined
Dec 16, 2019
Messages
114
Office Version
  1. 365
Platform
  1. Windows
I have a zip file that has a varying number of folders and in each folder there is a text file (.txt). What I would like to do is extract the names of those text files and place them in the second column of a Table called Table1. I can find code to extract names from a file folder but not from a zip file. Any assistance would be appreciated. Thanks
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Found an old post from Rick Rothstein to extract the folder names from the zip file. Would now need the code to extract the txt files from each folder. Thanks
VBA Code:
Sub ListZipDetails()
  Dim R As Long, PathFilename As Variant, FileNameInZip As Variant, oApp As Object
  PathFilename = Application.GetOpenFilename("ZipFiles (*.zip), *.zip")
  If PathFilename = "False" Then Exit Sub
  R = Cells(Rows.Count, "A").End(xlUp).Row
  Set oApp = CreateObject("Shell.Application")
  For Each FileNameInZip In oApp.Namespace(PathFilename).Items
    R = R + 1
    Cells(R, "A").Value = FileNameInZip
  Next
  Set oApp = Nothing
End Sub
 
Upvote 0
Found an old post from Rick Rothstein to extract the folder names from the zip file. Would now need the code to extract the txt files from each folder. Thanks
VBA Code:
Sub ListZipDetails()
  Dim R As Long, PathFilename As Variant, FileNameInZip As Variant, oApp As Object
  PathFilename = Application.GetOpenFilename("ZipFiles (*.zip), *.zip")
  If PathFilename = "False" Then Exit Sub
  R = Cells(Rows.Count, "A").End(xlUp).Row
  Set oApp = CreateObject("Shell.Application")
  For Each FileNameInZip In oApp.Namespace(PathFilename).Items
    R = R + 1
    Cells(R, "A").Value = FileNameInZip
  Next
  Set oApp = Nothing
End Sub
The following code works well if there is no sub-folder in the zip i.e. File.zip - Folder 1 - Sub-folder 1. When there is a sub-folder I get error 76 Path not found on line Set xFolder = xFSO.GetFolder(FolderNameInZip)
VBA Code:
Sub ListZipDetails()
  Dim R As Long, PathFilename As Variant, FolderNameInZip As Variant, oApp As Object
  Dim i As Integer
  Dim xFSO As Object, xFolder As Object, xFile As Object, Filename As Object
  PathFilename = Application.GetOpenFilename("ZipFiles (*.zip), *.zip")
  If PathFilename = "False" Then Exit Sub
  R = Cells(Rows.Count, "A").End(xlUp).Row
  Set oApp = CreateObject("Shell.Application")
  For Each FolderNameInZip In oApp.Namespace(PathFilename).Items
        Set xFSO = CreateObject("Scripting.FileSystemObject")
        Set xFolder = xFSO.GetFolder(FolderNameInZip)
        For Each xFile In xFolder.Files
        i = i + 1
        Cells(i, "A").Value = Replace(xFile.Name, ".txt", "")
        Next
  Next
  Set oApp = Nothing
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,911
Messages
6,122,199
Members
449,072
Latest member
DW Draft

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top