+ Reply to Thread
Results 1 to 3 of 3

zipping folders in selected folder

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-19-2019
    Location
    Michigan
    MS-Off Ver
    Office 365
    Posts
    547

    zipping folders in selected folder

    I have a process where I am creating folders with individual files then I want to take all of those folders and then zip them to individual files....it creates the files but is says 1kb meaning there is nothing inside, they are empty. Can someone help how to do this?

    Sub Test_ZipAllSubfoldersInFolder()
        ' Allow user to select parent folder
        Dim parentFolder As String
        Dim folderPicker As FileDialog
        Set folderPicker = Application.FileDialog(msoFileDialogFolderPicker)
    
        With folderPicker
            .Title = "Select Parent Folder"
            .AllowMultiSelect = False
            If .Show = -1 Then
                parentFolder = .SelectedItems(1)
            Else
                MsgBox "No folder selected, operation cancelled"
                Exit Sub
            End If
        End With
    
        ' Get the file system object
        Dim fso As Object
        Set fso = CreateObject("Scripting.FileSystemObject")
        Dim parentFolderObj As Object
        Set parentFolderObj = fso.GetFolder(parentFolder)
    
        ' Loop through each subfolder
        Dim subFolder As Object
        For Each subFolder In parentFolderObj.SubFolders
            Dim zipFileName As String
            zipFileName = parentFolder & "\" & subFolder.Name & ".zip"
            ZipAllFilesInFolder zipFileName, subFolder.Path
        Next subFolder
    End Sub
    
    Sub ZipAllFilesInFolder(zippedFileFullName As String, folderToZipPath As String)
        Dim ShellApp As Object
        Dim fso As Object
        Dim destinationFolder As Object
        Dim sourceFolder As Object
    
        Set ShellApp = CreateObject("Shell.Application")
        Set fso = CreateObject("Scripting.FileSystemObject")
    
        ' Create an empty zip file
        Open zippedFileFullName For Output As #1
        Print #1, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0)
        Close #1
    
        ' Add items to the zip file
        Set destinationFolder = ShellApp.Namespace(zippedFileFullName)
        Set sourceFolder = ShellApp.Namespace(folderToZipPath)
    
        If Not destinationFolder Is Nothing And Not sourceFolder Is Nothing Then
            destinationFolder.CopyHere sourceFolder.Items
    
            ' Wait until all items are copied
            Dim start As Single
            start = Timer
            Do Until Timer - start > 10 Or _
                destinationFolder.Items.Count >= sourceFolder.Items.Count
                DoEvents
            Loop
        End If
    
        ' Clean up
        Set destinationFolder = Nothing
        Set sourceFolder = Nothing
        Set fso = Nothing
        Set ShellApp = Nothing
    End Sub

  2. #2
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,493

    Re: zipping folders in selected folder

    Deleted by me
    Last edited by daboho; 05-04-2024 at 02:04 PM.
    "Presh Star Who has help you *For Add Reputation!! And mark case as Solve"

  3. #3
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,493

    Re: zipping folders in selected folder

    Option Compare Database
    Option Explicit
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    'Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 'for 64 bit
    
    Sub ZipAllFilesInFolder(zippedFileFullName As String, folderToZipPath As String)
        Dim ShellApplication As Object
        Dim fso As Object
        Dim destinationFolder As Object
        Dim sourceFolder As Object
    
        Set ShellApplication = CreateObject("Shell.Application")
       'Check for zipFolder has created
        If Dir(zippedFileFullName, vbDirectory) = "" Then
             MkDir zippedFileFullName
        End If
        ' Create an empty zip file
        Open zippedFileFullName For Output As #1
        Print #1, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0)
        Close #1
    
        ' Add items to the zip file
        
         With ShellApplication
            Debug.Print Timer, "zipping started ..."
            .NameSpace(CVar(zippedFileFullName)).CopyHere CVar(folderToZipPath)
            On Error Resume Next
            Do Until .NameSpace(CVar(zippedFileFullName)).Items.Count = 1
                'DoEvents
                Sleep 100
                Debug.Print " .";
            Loop
            Debug.Print
            On Error GoTo 0
            Debug.Print Timer, "Zip done."
        End With
        ' Clean up
        Set ShellApplication = Nothing
    End Sub
    'call sub macro
    Sub Test_ZipAllSubfoldersInFolder()
        ' Allow user to select parent folder
        Dim parentFolder As String
        Dim folderPicker As FileDialog
        Set folderPicker = Application.FileDialog(msoFileDialogFolderPicker)
    
        With folderPicker
            .Title = "Select Parent Folder"
            .AllowMultiSelect = False
            If .Show = -1 Then
                parentFolder = .SelectedItems(1)
            Else
                MsgBox "No folder selected, operation cancelled"
                Exit Sub
            End If
        End With
    
        ' Get the file system object
        Dim fso As Object
        Set fso = CreateObject("Scripting.FileSystemObject")
        Dim parentFolderObj As Object
        Set parentFolderObj = fso.GetFolder(parentFolder)
    
        ' Loop through each subfolder
        Dim subFolder As Object
        For Each subFolder In parentFolderObj.SubFolders
            Dim zipFileName As String
            zipFileName = parentFolder & "\" & subFolder.Name & ".zip"
            ZipAllFilesInFolder zipFileName, subFolder.Path
        Next subFolder
    End Sub
    Last edited by daboho; 05-04-2024 at 08:03 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Zipping a folder which is placed on a server
    By Farley945 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-14-2023, 06:53 AM
  2. Replies: 2
    Last Post: 09-02-2022, 10:32 PM
  3. Merge All Sheets From All Selected Folders Into One Folder
    By CoSinus in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-20-2021, 08:41 AM
  4. Macro to list all files and folders in sub folders and choose a starting folder
    By LeanAccountant in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-06-2021, 02:04 PM
  5. copy checkbox selected listed files from base folder to user selected folder
    By ayush842001 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-16-2020, 08:23 AM
  6. [SOLVED] Count only the current folders in a folder, skip hidden & system folders
    By CobraLAD in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-03-2015, 04:58 AM
  7. Zipping file from folder containing name
    By mduff3 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-29-2014, 04:43 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1