How to copy /unzipped files within zip files to existing folder

ryan8200

Active Member
Joined
Aug 21, 2011
Messages
357
Hi All,

I have 2 zip folders in current directory. I want to copy all csv files within the zip folder to current directory (same directory with zip folders). I try the following code but it only copy zip folder and not csv files within the zip folder. Kindly assist. Thanks.
VBA Code:
Sub Unzipfiles()
Dim sh As New Shell32.Shell

Path = "C:\Users\My\Downloads\"
Filename = Dir(Path & "*.zip")

' Disable Screen Updating is used to stop screen flickering and Disable Events is used to avoid interrupted dialog boxes / popups
With Application
    .ScreenUpdating = False
    .EnableEvents = False
End With

  Do While Filename <> ""
     Filename = Dir()
  Loop
  
  'Destination & Source Path
  sh.Namespace(Path).CopyHere _
    sh.Namespace(Path & Filename).Items, 16

End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Maybe:
VBA Code:
Option Explicit
Sub UnzipAFile()     ' Unzip a zip file to a folder
    'Declare variables
    Dim sh     As Object
    Dim Filename As Variant, Path As Variant
    ' Disable Screen Updating is used to stop screen flickering and Disable Events is used to avoid interrupted dialog boxes / popups
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With
    Set sh = CreateObject("Shell.Application")
    Path = "C:\Users\My\Downloads\"
    Filename = Dir(Path & "*.zip")
    Do While Filename <> ""
        ' Destination & Source Path
        sh.Namespace(Path).CopyHere sh.Namespace(Path & Filename).Items
        Filename = Dir()
    Loop
    ' Resume Screen Updating and Events
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
    Set sh = Nothing
    MsgBox "Done!"
End Sub
 
Upvote 0
Maybe:
VBA Code:
Option Explicit
Sub UnzipAFile()     ' Unzip a zip file to a folder
    'Declare variables
    Dim sh     As Object
    Dim Filename As Variant, Path As Variant
    ' Disable Screen Updating is used to stop screen flickering and Disable Events is used to avoid interrupted dialog boxes / popups
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With
    Set sh = CreateObject("Shell.Application")
    Path = "C:\Users\My\Downloads\"
    Filename = Dir(Path & "*.zip")
    Do While Filename <> ""
        ' Destination & Source Path
        sh.Namespace(Path).CopyHere sh.Namespace(Path & Filename).Items
        Filename = Dir()
    Loop
    ' Resume Screen Updating and Events
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
    Set sh = Nothing
    MsgBox "Done!"
End Sub
Hi Rollis13,
Thanks for your input. Your code really work in some extent. How to enhance the above code, so that it will copy csv files contains inside multiple folders within each zip folder ?
 
Upvote 0
Do you mean that inside the zipped file you have .cvs files and other zipped files ? or do you mean that inside the zipped file you have .cvs files and other folders that contain other .cvs files ?
 
Upvote 0
Do you mean that inside the zipped file you have .cvs files and other zipped files ? or do you mean that inside the zipped file you have .cvs files and other folders that contain other .cvs files ?
Inside the zipped file have .cvs files and other folders that contain other .cvs files
 
Upvote 0
Have a try:
VBA Code:
Option Explicit
Sub UnzipAFile_new()
    Dim Filename As Variant, Path As Variant
    Dim sh     As Object
    Dim fso    As Object, SubFldr As Object
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With
    Set sh = CreateObject("Shell.Application")
    Set fso = CreateObject("Scripting.FileSystemObject")
    Path = "C:\Users\My\Downloads\"
    Filename = Dir(Path & "*.zip")
    'unzip zip files to a folder (files & subfolders)
    Do While Filename <> ""
        sh.Namespace(Path).CopyHere sh.Namespace(Path & Filename).Items
        DoEvents
        Filename = Dir()
    Loop
    'copy files from subfolders to folder
    For Each SubFldr In fso.GetFolder(Path).SubFolders
        fso.CopyFile Source:=SubFldr & "\" & "*.csv", Destination:=Path
    Next
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
    Set sh = Nothing
    Set fso = Nothing
    MsgBox "Done!"
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,954
Messages
6,122,461
Members
449,085
Latest member
ExcelError

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