Solved

Help adding files to a ZIP function

Posted on 2009-04-13
7
238 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:Syncer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
7 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24153540
What kind of help can I provide for you?
0
 

Author Comment

by:Syncer
ID: 24153595
How can I pass multiple paths to srcStringFolder and it add all passed to the ZIP?
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 24153925
I would think that you could define a string list for the source folder names, and then put this code in For loop:

Dim srcFolderList As New List(Of String)

srcFolderList.Add("C:\MyFolder1")
srcFolderList.Add("C:\MyFolder2")
      Dim objShell As New Shell32.ShellClass
      Dim objFolderSrc As Shell32.Folder
      Dim objFolderDst As Shell32.Folder
      Dim objFolderItems As Shell32.FolderItems
 
      For Each srcFolderString in srcFolderList
        objFolderSrc = objShell.NameSpace(srcfolderString)
        objFolderDst = objShell.NameSpace(dstfolderString)
        objFolderItems = objFolderSrc.Items
        objFolderDst.CopyHere(objFolderItems, 20)
      Next srcFolderString

Open in new window

0
 

Author Comment

by:Syncer
ID: 24153967
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

0
 

Author Comment

by:Syncer
ID: 24154150
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!
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question