Link to home
Start Free TrialLog in
Avatar of Syncer
SyncerFlag for United States of America

asked on

Help adding files to a ZIP function

I found a script on a forum that allows a user to click a button, select a folder from a FolderBrowserDialog, and create a ZIP file from that folder.

I need to modify that function to cycle through a small set of checkboxes and based on the selections, add each corresponding folder/file that is checked to the new ZIP.

My problem is that being my second week in Visual Basic, I'm still too great to know where to start.

The code that I have currently is listed below.
Private Sub ButtonGatherLogs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonGatherLogs.Click
        Dim FolderOneString As String
        Dim FolderTwoString As String
        Dim SingleFileString As String
        Dim srcfolderString As String
        Dim dstfolderString As String = (My.Computer.FileSystem.SpecialDirectories.Desktop + "\Logs.zip")
 
        'get folder to zip
        If CheckBoxFolderOneDirectory.CheckState = CheckState.Checked Then
            'Add FolderOne to ZIP
            FolderOneString = (My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData + "\FolderOne")
        End If
        If CheckBoxFolderTwo.CheckState = CheckState.Checked Then
            'Add FolderTwoDirectory to ZIP
            FolderTwoString = (My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData + "\FolderTwo")
        End If
        If CheckBoxSingleFile.CheckState = CheckState.Checked Then
            'Add console log to ZIP
            SingleFile = (My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData + "\file.log")
        End If
 
        srcfolderString = 'Add each string above to the list based on checkbox selection
 
        If srcfolderString = "" Then Exit Sub
 
        'create empty zip file
        Dim fileContents() As Byte = {80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
        My.Computer.FileSystem.WriteAllBytes(dstfolderString, fileContents, False)
 
        Dim objShell As New Shell32.ShellClass
        Dim objFolderSrc As Shell32.Folder
        Dim objFolderDst As Shell32.Folder
        Dim objFolderItems As Shell32.FolderItems
 
        objFolderSrc = objShell.NameSpace(srcfolderString)
        objFolderDst = objShell.NameSpace(dstfolderString)
        objFolderItems = objFolderSrc.Items
        objFolderDst.CopyHere(objFolderItems, 20)
 
    End Sub

Open in new window

Avatar of Bob Learned
Bob Learned
Flag of United States of America image

What kind of help can I provide for you?
Avatar of Syncer

ASKER

How can I pass multiple paths to srcStringFolder and it add all passed to the ZIP?
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Syncer

ASKER

Using the above method, I now get an unhandled NullReferenceException at objFolderItems = objFolderSrc.Items - "Object reference not set to an instance of an object."
Private Sub ButtonGatherLogs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonGatherLogs.Click
        Dim srcFolderList As New List(Of String)
        Dim srcfolderString As String
        Dim dstfolderString As String = (My.Computer.FileSystem.SpecialDirectories.Desktop + "\Logs.zip")
        Dim objShell As New Shell32.ShellClass
        Dim objFolderSrc As Shell32.Folder
        Dim objFolderDst As Shell32.Folder
        Dim objFolderItems As Shell32.FolderItems
 
        'get folder to zip
        If CheckBoxFolderOne.CheckState = CheckState.Checked Then
            'Add FolderOneto ZIP
            srcFolderList.Add(My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData + "\FolderOne")
        End If
        If CheckBoxFolderTwo.CheckState = CheckState.Checked Then
            'Add FolderTwo Directory to ZIP
            srcFolderList.Add(My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData + "\FolderTwo")
        End If
        If CheckBoxFileOne.CheckState = CheckState.Checked Then
            'Add FileOne to ZIP
            srcFolderList.Add(My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData + "\FileOne.log")
        End If
 
        If CheckBoxSyncFolderOne.CheckState Or CheckBoxFolderTwo.CheckState Or CheckBoxFileOne.CheckState = CheckState.Checked Then
            'create empty zip file
            Dim fileContents() As Byte = {80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
            My.Computer.FileSystem.WriteAllBytes(dstfolderString, fileContents, False)
 
            For Each srcfolderString In srcFolderList
                objFolderSrc = objShell.NameSpace(srcfolderString)
                objFolderDst = objShell.NameSpace(dstfolderString)
                objFolderItems = objFolderSrc.Items
                objFolderDst.CopyHere(objFolderItems, 20)
            Next srcfolderString
        End If
    End Sub

Open in new window

Avatar of Syncer

ASKER

It turns out it was just my fault:
   My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData
Needed to be renamed to:
   Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

Thanks for the TheLearnedOne!