0

So I have two functions that need help.

Public Function checkZipForFiles(ByVal zipFilepath As String)
    Try
        Dim doc As New System.Xml.XmlDocument
        If My.Computer.FileSystem.FileExists("Backup.xml") Then
            doc.Load("Backup.xml")

            Dim JobNodes As XmlNodeList = doc.GetElementsByTagName("Job")
            For Each JobNode In JobNodes
                Dim Source = JobNode.SelectNodes("Source")

                For Each item As System.Xml.XmlNode In Source
                    For Each File In checkFilesInFolder(item.InnerText)
                        Using zip = ZipFile.Read(zipFilepath)
                            Dim fileName As String
                            fileName = checkFilesInFolder(item.InnerText)
                            Dim e As ZipEntry = zip(fileName)

                            If e Is Nothing Then
                                Console.WriteLine("File: " & fileName & " does not exist in zip.")
                            End If
                        End Using
                    Next

                Next
            Next
        End If
    Catch ex As Exception
        Console.Error.WriteLine(ex.Message)
        myLogger.Log(ex.Message)
    End Try

End Function

This one reads in an xml file. The xml file stores the information for the zipping process such as "Destination", "File Source", and "Job name". I want this function to check the the zip to see if all of the files are in the zip. As you can see this function needs the aid of "checkFilesiInFolder" function to get the filenames to search for in the zip.

PROBLEM - I only get returned the last file that is scanned in the "checkFilesInFolder" function.

Public Function checkFilesInFolder(ByVal folderPath As String)
    Try
        ' make a reference to a directory
        Dim di As New IO.DirectoryInfo(folderPath)
        Dim diar1 As IO.FileInfo() = di.GetFiles()
        Dim file As IO.FileInfo

        Console.WriteLine("The following files are located in " & folderPath)

        'list the names of all files in the specified directory
        For Each file In diar1
            Console.WriteLine(file.FullName)
            'myLogger.Log(file.ToString)

        Next
        Return file.ToString
    Catch ex As Exception
        Console.Error.WriteLine(ex.Message)
        myLogger.Log(ex.Message)
    End Try
End Function
MaylorTaylor
  • 4,671
  • 16
  • 47
  • 76

1 Answers1

0

Your checkFilesInFolder function should return a collection instead of just the string. Try changing its definition to something like:

Public Function checkFilesInFolder(ByVal folderPath As String) As List(Of String)
    Dim returnList As List(Of String) = New List(Of String)()

    Try
        ' make a reference to a directory
        Dim di As New IO.DirectoryInfo(folderPath)
        Dim diar1 As IO.FileInfo() = di.GetFiles()
        Dim file As IO.FileInfo

        Console.WriteLine("The following files are located in " & folderPath)

        'list the names of all files in the specified directory
        For Each file In diar1
            Console.WriteLine(file.FullName)
            Console.WriteLine(file.Name)
            returnList.Add(file.Name) 'Or FullName, depending on what you want to use
        Next
    Catch ex As Exception
        Console.Error.WriteLine(ex.Message)
    End Try

    Return returnList
End Function

Note that we are using the List collection class to hold all the file names. This will let your For Each File In checkFilesInFolder(item.InnerText) statement work appropriately.

I am not sure, from your use case, whether you need to add file.Name or file.FullName to returnList, but I would suggest you debug through or otherwise try with both and see which one works for you.

Now that we have this list we can:

  1. Go through each file
  2. Check that file is present in the ZIP or not

So it seems, at first blush, your checkZipForFiles method is doing the right things, but your For loop probably just needs some tweaking to work with the new collection:

    For Each item As System.Xml.XmlNode In Source
        For Each fileName As String In checkFilesInFolder(item.InnerText)
            Using zip = ZipFile.Read(zipFilepath)
                Dim e As ZipEntry = zip(fileName)

                If e Is Nothing Then
                    Console.WriteLine("File: " & fileName & " does NOT exist in zip.")
                Else
                    Console.WriteLine("File: " & fileName & " does EXIST in zip.")
                End If
            End Using
        Next
    Next

Note some things here: We get a List(Of String) out of checkFilesInFolder so your For Each is just dealing with a bunch of strings - in this case they are file names which which can be checked in the ZIP file by feeding it to zip like you were already doing.

nkvu
  • 5,551
  • 2
  • 16
  • 12