Unzip a folder and copy all the files inside the folder to a different folder

mali10020

New Member
Joined
Oct 15, 2021
Messages
17
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
Good morning guys!!

please assist if you can!!

I want this code to unzip the latest folder in Dir and copy all the files inside that folder to a different folder and open the bigest file in the folder

Sub Unzip1()
Dim FSO As Object
Dim oApp As Object
Dim Fname As Variant
Dim FileNameFolder As Variant
Dim DefPath As String
Dim strDate As String

Fname = Application.GetOpenFilename(filefilter:="Zip Files (*.zip), *.zip", _
MultiSelect:=False)
If Fname = False Then
'Do nothing
Else
'Root folder for the new folder.
'You can also use DefPath = "C:\Apps\CipherRounds_1633694385.zip"
DefPath = Application.DefaultFilePath
If Right(DefPath, 1) <> "\" Then
DefPath = DefPath & "\"
End If

'Create the folder name
strDate = Format(Now, " dd-mm-yy h-mm-ss")
FileNameFolder = DefPath & "MyUnzipFolder " & strDate & "\"

'Make the normal folder in DefPath
MkDir FileNameFolder

'Extract the files into the newly created folder
Set oApp = CreateObject("Shell.Application")

oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(Fname).Items

'If you want to extract only one file you can use this:
'oApp.Namespace(FileNameFolder).CopyHere _
'oApp.Namespace(Fname).items.Item("test.txt")

MsgBox "You find the files here: " & FileNameFolder

On Error Resume Next
Set FSO = CreateObject("scripting.filesystemobject")
FSO.DeleteFolder Environ("Temp") & "\Temporary Directory*", True
End If

'ActiveWorkbook.Worksheets("").CopyWorkbooks("Mobile").Worksheets ("Device name")
'ActiveSheet.name = NewestFile.name
Cells.Select
Selection.Copy
Application.DisplayAlerts = False

ActiveWindow.Close

Application.CutCopyMode = False
Application.DisplayAlerts = True

Windows("Mobile.xlsm").Activate
ActiveSheet.Paste
Range("A1").Select


'ActiveSheet.name = NewestFile.name
'Workbooks(NewestFile.Name).Close

End Sub
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
See if this does what you want:

VBA Code:
Sub Unzip2()
'
    Dim LargestFileSize As Long
    Dim FSO             As Object
    Dim oApp            As Object
    Dim DefPath         As String
    Dim LargestFile     As String
    Dim MyFile          As String
    Dim strDate         As String
    Dim Fname           As Variant
    Dim FileNameFolder  As Variant
'
    Fname = Application.GetOpenFilename(filefilter:="Zip Files (*.zip), *.zip", MultiSelect:=False)
'
    If Fname = False Then
        Exit Sub
    Else
'       Root folder for the new folder.
'       You can also use DefPath = "C:\Apps\CipherRounds_1633694385.zip"
        DefPath = Application.DefaultFilePath
'
        If Right(DefPath, 1) <> "\" Then DefPath = DefPath & "\"
'
'       Create the folder name
        strDate = Format(Now, " dd-mm-yy h-mm-ss")
        FileNameFolder = DefPath & "MyUnzipFolder " & strDate & "\"
'
'       Make the normal folder in DefPath
        MkDir FileNameFolder
'
'       Extract the files into the newly created folder
        Set oApp = CreateObject("Shell.Application")
        oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(Fname).Items
'
'       If you want to extract only one file you can use this:
'       oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(Fname).items.Item("test.txt")
'
        MsgBox "You find the files here: " & FileNameFolder
    End If
'
    LargestFileSize = 0
'
    MyFile = Dir(FileNameFolder & "*.*")
'
    If Len(MyFile) = 0 Then
        MsgBox "No files were found...", vbExclamation
        Exit Sub
    End If
'
    Do While Len(MyFile) > 0
        If FileLen(FileNameFolder & MyFile) >= LargestFileSize Then
            LargestFile = MyFile
            LargestFileSize = FileLen(FileNameFolder & MyFile)
        End If
'
        MyFile = Dir
    Loop
'
'   Temporary Troubleshoot message
    MsgBox "The Largest file is named: " & LargestFile & " and it's size is " & LargestFileSize & " bytes"
'
    Workbooks.Open FileNameFolder & LargestFile
End Sub

It asks the user for the zip file location, extracts the files in the zip file to a folder and then opens the largest file that was extracted from the zip file.
 
Upvote 0
Thank you so much for your help!!

it did not work..

i forget to means there is a folder inside the zip folder inside that folder all the data files.



report.PNG
 
Upvote 0
Ok, Assuming you have a zip file with one folder containing all of the files:

VBA Code:
Sub Unzip3()
'
'   Assuming your zip file only has 1 folder
'
    Dim FolderCount     As Long
    Dim FolderIndex     As Long
    Dim LargestFileSize As Long
    Dim oApp            As Object
    Dim objFolder       As Object
    Dim objFolders      As Object
    Dim objFSO          As Object
    Dim arrFolders()    As String
    Dim DefPath         As String
    Dim LargestFile     As String
    Dim MyFile          As String
    Dim strDate         As String
    Dim SubFolderName   As String
    Dim Fname           As Variant
    Dim FileNameFolder  As Variant
'
    Fname = Application.GetOpenFilename(filefilter:="Zip Files (*.zip), *.zip", MultiSelect:=False)
'
    If Fname = False Then
        Exit Sub
    Else
'       Root folder for the new folder.
'       You can also use DefPath = "C:\Apps\CipherRounds_1633694385.zip"
        DefPath = Application.DefaultFilePath
'
        If Right(DefPath, 1) <> "\" Then DefPath = DefPath & "\"
'
'       Create the folder name
        strDate = Format(Now, " dd-mm-yy h-mm-ss")
        FileNameFolder = DefPath & "MyUnzipFolder " & strDate & "\"
'
'       Make the normal folder in DefPath
        MkDir FileNameFolder
'
'       Extract the files into the newly created folder
        Set oApp = CreateObject("Shell.Application")
        oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(Fname).Items
'
'       If you want to extract only one file you can use this:
'       oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(Fname).items.Item("test.txt")
'
        MsgBox "You find the files here: " & FileNameFolder
    End If
'
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolders = objFSO.GetFolder(FileNameFolder).SubFolders
'
    FolderCount = objFolders.Count
'
    If FolderCount > 0 Then
        ReDim arrFolders(1 To FolderCount)
        FolderIndex = 0
'
        For Each objFolder In objFolders
            FolderIndex = FolderIndex + 1
            arrFolders(FolderIndex) = objFolder.Name
        Next objFolder
'
        SubFolderName = arrFolders(FolderIndex) & "\"
    Else
        MsgBox "No folders found!", vbExclamation
    End If
'
    LargestFileSize = 0
'
    MyFile = Dir(FileNameFolder & SubFolderName & "*.*")
'
    If Len(MyFile) = 0 Then
        MsgBox "No files were found...", vbExclamation
        Exit Sub
    End If
'
    Do While Len(MyFile) > 0
        If FileLen(SubFolderName & MyFile) >= LargestFileSize Then
            LargestFile = MyFile
            LargestFileSize = FileLen(SubFolderName & MyFile)
        End If
'
        MyFile = Dir
    Loop
'
'   Temporary Troubleshoot message
    MsgBox "The Largest file is named: " & LargestFile & " and it's size is " & LargestFileSize & " bytes"
'
''    Workbooks.Open FileNameFolder & LargestFile
    Workbooks.Open SubFolderName & LargestFile
End Sub
 
Upvote 0
Good morning John,

Thank you so much for your help!!

I worked.. put it keep asking me to select the zip folder path I want everything to run in the backend.
 
Upvote 0
If the location and name of the zip file will not change then:

VBA Code:
Sub Unzip3_5()
'
'   Assuming your zip file only has 1 folder
'
    Dim FolderCount     As Long
    Dim FolderIndex     As Long
    Dim LargestFileSize As Long
    Dim oApp            As Object
    Dim objFolder       As Object
    Dim objFolders      As Object
    Dim objFSO          As Object
    Dim arrFolders()    As String
    Dim DefPath         As String
    Dim LargestFile     As String
    Dim MyFile          As String
    Dim strDate         As String
    Dim SubFolderName   As String
    Dim ZipFilePath     As String
    Dim Fname           As Variant
    Dim FileNameFolder  As Variant
'
    ZipFilePath = "C:\Users\Laptop\Downloads\"                                                      ' <--- Set this to the path to the zip file
    DesiredZipFile = "CipherRounds_1633694385.zip"                                                  ' <--- Set this to the zip file name to unzip
'
    Fname = ZipFilePath & DesiredZipFile
'
'   Root folder for the new folder.
'   You can also use DefPath = "C:\Apps\CipherRounds_1633694385.zip"
    DefPath = Application.DefaultFilePath
'
    If Right(DefPath, 1) <> "\" Then DefPath = DefPath & "\"
'
'   Create the folder name
    strDate = Format(Now, " dd-mm-yy h-mm-ss")
    FileNameFolder = DefPath & "MyUnzipFolder " & strDate & "\"
'
'   Make the normal folder in DefPath
    MkDir FileNameFolder
'
'   Extract the files into the newly created folder
    Set oApp = CreateObject("Shell.Application")
    oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(Fname).Items
'
'   If you want to extract only one file you can use this:
'   oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(Fname).items.Item("test.txt")
'
    MsgBox "You find the files here: " & FileNameFolder
'
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolders = objFSO.GetFolder(FileNameFolder).SubFolders
'
    FolderCount = objFolders.Count
'
    If FolderCount > 0 Then
        ReDim arrFolders(1 To FolderCount)
        FolderIndex = 0
'
        For Each objFolder In objFolders
            FolderIndex = FolderIndex + 1
            arrFolders(FolderIndex) = objFolder.Name
        Next objFolder
'
        SubFolderName = arrFolders(FolderIndex) & "\"
    Else
        MsgBox "No folders found!", vbExclamation
    End If
'
    LargestFileSize = 0
'
    MyFile = Dir(FileNameFolder & SubFolderName & "*.*")
'
    If Len(MyFile) = 0 Then
        MsgBox "No files were found...", vbExclamation
        Exit Sub
    End If
'
    Do While Len(MyFile) > 0
        If FileLen(SubFolderName & MyFile) >= LargestFileSize Then
            LargestFile = MyFile
            LargestFileSize = FileLen(SubFolderName & MyFile)
        End If
'
        MyFile = Dir
    Loop
'
'   Temporary Troubleshoot message
    MsgBox "The Largest file is named: " & LargestFile & " and it's size is " & LargestFileSize & " bytes"
'
''    Workbooks.Open FileNameFolder & LargestFile
    Workbooks.Open SubFolderName & LargestFile
End Sub
 
Upvote 0
The zip folder name always change

I'm getting this error messgae " Compile error: Variable not defind" I think this is the issue DesiredZipFile = "C:\Apps\CipherRounds_1633694385.zip\CipherRounds"

Sub Unzip3_5()
'
' Assuming your zip file only has 1 folder
'
Dim FolderCount As Long
Dim FolderIndex As Long
Dim LargestFileSize As Long
Dim oApp As Object
Dim objFolder As Object
Dim objFolders As Object
Dim objFSO As Object
Dim arrFolders() As String
Dim DefPath As String
Dim LargestFile As String
Dim MyFile As String
Dim strDate As String
Dim SubFolderName As String
Dim ZipFilePath As String
Dim Fname As Variant
Dim FileNameFolder As Variant
'
ZipFilePath = "C:\Apps\CipherRounds_1633694385.zip" ' <--- Set this to the path to the zip file
DesiredZipFile = "C:\Apps\CipherRounds_1633694385.zip\CipherRounds" ' <--- Set this to the zip file name to unzip
'
Fname = ZipFilePath & DesiredZipFile
'
' Root folder for the new folder.
' You can also use DefPath = "C:\Apps\CipherRounds_1633694385.zip"
DefPath = Application.DefaultFilePath
'
If Right(DefPath, 1) <> "\" Then DefPath = DefPath & "\"
'
' Create the folder name
strDate = Format(Now, " dd-mm-yy h-mm-ss")
FileNameFolder = DefPath & "MyUnzipFolder " & strDate & "\"
'
' Make the normal folder in DefPath
MkDir FileNameFolder
'
' Extract the files into the newly created folder
Set oApp = CreateObject("Shell.Application")
oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(Fname).Items
'
' If you want to extract only one file you can use this:
' oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(Fname).items.Item("test.txt")
'
MsgBox "You find the files here: " & FileNameFolder
'
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolders = objFSO.GetFolder(FileNameFolder).SubFolders
'
FolderCount = objFolders.Count
'
If FolderCount > 0 Then
ReDim arrFolders(1 To FolderCount)
FolderIndex = 0
'
For Each objFolder In objFolders
FolderIndex = FolderIndex + 1
arrFolders(FolderIndex) = objFolder.Name
Next objFolder
'
SubFolderName = arrFolders(FolderIndex) & "\"
Else
MsgBox "No folders found!", vbExclamation
End If
'
LargestFileSize = 0
'
MyFile = Dir(FileNameFolder & SubFolderName & "*.*")
'
If Len(MyFile) = 0 Then
MsgBox "No files were found...", vbExclamation
Exit Sub
End If
'
Do While Len(MyFile) > 0
If FileLen(SubFolderName & MyFile) >= LargestFileSize Then
LargestFile = MyFile
LargestFileSize = FileLen(SubFolderName & MyFile)
End If
'
MyFile = Dir
Loop
'
' Temporary Troubleshoot message
MsgBox "The Largest file is named: " & LargestFile & " and it's size is " & LargestFileSize & " bytes"
'
'' Workbooks.Open FileNameFolder & LargestFile
Workbooks.Open SubFolderName & LargestFile
End Sub
 
Upvote 0
Add the following line to the Dim section at the top:

VBA Code:
Dim DesiredZipFile As String
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,242
Members
452,623
Latest member
russelllowellpercy

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