Help Needed for Data Extraction from Hundreds of Zipped Files!

adgezi

New Member
Joined
Nov 24, 2015
Messages
2
I'm now in a headache to extract data from files and really wish somebody could help me out of this.

The problem is that there is a directory, e.g. I:\john

In this directory, there are 100 zipped files. In each zipped file, there are 26 different .csv files.

I only want to extract the data from one of these files. The file includes the data I want has the file name starting with ABC_DATA_.

Then similarly, repeat this process to extract this data from all 100 zipped files.

All the extracted data might be stored in a new excel or text file.

Really appreciate it for your guidance or help if somebody could provide some sample VBA code for this problem.

John
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
The code create temp directory where the workbook located. It iterates over each zip files, unpack its content to temp dir. Then you insert your logic of processing them. csvFile is File object. It has Path property which gives you full path to this extracted file. After you process it, code cleans up temp folder.
Code:
Sub ProcessCSV()


    Dim ofile, o, fso As Object, zipFile, zipFile2, csvFile, tempDir
        
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    'Create temp directory where the workbook is located
    tempDir = ThisWorkbook.Path & "\UNZIPPED_FILES"
    On Error Resume Next
    MkDir tempDir
    On Error GoTo 0
    
    ' Iterate over each zip file in "I:\John" folder
    For Each zipFile In fso.GetFolder("I:\john").Files


        'Copy each file to the destination folder
        Set o = CreateObject("Shell.Application")
        For Each ofile In o.Namespace(zipFile.Path).items
            o.Namespace(tempDir).CopyHere (ofile)
        Next
        Set o = Nothing
        
        For Each csvFile In fso.GetFolder(tempDir).Files
            If csvFile.Name Like "ABC_DATA_*" Then Exit For
        Next
        
        'Here we have our csvFile File object
        'Do something with them...
        
        
        'Delete every file in temp folder after processing CSV files
        For Each zipFile2 In fso.GetFolder(tempDir).Files
            zipFile2.Delete True
        Next
        
    Next
    
    fso.DeleteFolder tempDir, True 'Delete temp folder


End Sub
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,192
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