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
 
I guess I am confused how you expect the script to run by itself if the locations and names will be changing.
 
Upvote 0

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
The location does not change just the name of the folder chnage
I can't wrap my head around that logic

Perhaps if you give some example folder locations where the zip file might be located I may be able to come up with something for you.

example: C:\Users\Desktop\Temp
C:\Users\Desktop\Perm
etc.

Will there be more than one zip file in the folder?

Examples of possible zip file names?
 
Upvote 0
Good afternoon John,

The location does not change.

The Zip folder and the files names always change
The folder inside the zip folder the name does not change
Will there be more than one zip file in the folder? The files are not copressed just the folder.
report.PNG
 
Upvote 0
Try this:

VBA Code:
Sub Unzip4()
'
'   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 ZipFileName     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
    ZipFileName = Dir(ZipFilePath & "*.zip")                                                                ' Get the name of zip file
'
    Fname = ZipFilePath & ZipFileName
'
'   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 SubFolderName & LargestFile                                                              ' Open the largest file
End Sub

You only need to set the path to the zip file.
 
Upvote 0
Solution
Good evening @johnnyL!!

I end up using this code but every time " run time error "76" path not found.
1634868399145.png

I'm not sure If you have time to setup a zoom meeting..

Thank you so much !!
Sub Unzip3()

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\Mobile_1634867308.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
VBA Code:
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
 

Attachments

  • unzip3.PNG
    unzip3.PNG
    5.9 KB · Views: 11
Upvote 0
@johnnyL They change the report the files are not compress only the folder.

for some reason the unzip3 only worked with 1 pacific folder.. not sure why


Thanks!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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