Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Merge Multiple FileInfo Arrays

Posted on 2011-04-29
2
Medium Priority
?
978 Views
Last Modified: 2013-11-26
I am using the DirectoryInfo.GetFiles method to search directories for a certain file type.  The GetFiles method returns a FileInfo file list.  I have several directories that I need to search, so I need to combine several FileInfo arrays that I need to combine.  What is the best way to combine these arrays, or is there a better solution?

Here is the code.  I have a loop where I am passing a path into the function, and it find all of the PDFs in the subdirectories.  I need to combine the FileInfo arrays in the loop.

Private Function FindAllPDFsInArchives(ByVal Archives As DataTable) As FileInfo()
       
        Dim strArchiveFullPath As String

        For i As Integer = 0 To Archives.Rows.Count - 1
            strArchiveFullPath = Archives.Rows(i).Item("PATH").ToString.Trim()
       
            aAllDocsInArchive = SelectAllDocsInArchive(

        Next

        Return allDocsInArchive
    End Function

Public Function SelectAllDocsInArchive(ByVal DirPath as String) As FileInfo()

        Dim archiveFolder As DirectoryInfo
        Dim findFiles As FileInfo() = Nothing

        Dim strFullPathToDoc As String = DirPath

        archiveFolder = New DirectoryInfo(strFullPathToDoc)

        Try
            findFiles = archiveFolder.GetFiles("*.PDF", SearchOption.AllDirectories)
        Catch ex As Exception
            Throw ex
        End Try

        Return findFiles
    End Function

Open in new window

0
Comment
Question by:frontback45
2 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35491870
Why not create a List(Of FileInfo) that you can append to:
Imports System.Collections.Generic
...

Dim wholeList As New List(Of FileInfo)

...

wholeList.AddRange(archiveFolder.GetFiles("*.PDF", SearchOption.AllDirectories))

Open in new window

0
 
LVL 16

Accepted Solution

by:
HooKooDooKu earned 2000 total points
ID: 35492495
If you want to stick with arrays, you will need to work with two arrays, resize one, and copy the contents of the other to the first.  I'll warn you that such logic is typically very inefficient as the resizing of an array causes a whole new memory block to be allocated and then copies all the old array contents to the new array.  That's fine for a few iterations.  But if you have lots of iteration, it can result in copying the same data over and over.

However, to accomplish this, your top function should look something like the code below.  I will warn you that I am not a VB.net expert, so you'll need to step through the code and make sure the code below doesn't have a bug because I might have something off by 1 (over the years things have not been consistant between the various languages as to wether an array starts at 0 or starts at 1).
Public Function FindAllPDFsInArchives(ByVal Archives As DataTable) As FileInfo()

        Dim strArchiveFullPath As String
        Dim aAllDocsInArchive As FileInfo()

        For i As Integer = 0 To Archives.Rows.Count - 1
            strArchiveFullPath = Archives.Rows(i).Item("PATH").ToString.Trim()

            Dim aSomeDocsInArchive As FileInfo()
            aSomeDocsInArchive = SelectAllDocsInArchive(strArchiveFullPath)

            Dim nOrgLen = aAllDocsInArchive.Length
            Array.Resize(aAllDocsInArchive, nOrgLen + aSomeDocsInArchive.Length)
            For j As Integer = 0 To aSomeDocsInArchive.Length - 1
                aAllDocsInArchive(nOrgLen + j) = aSomeDocsInArchive(j)
            Next

        Next

        Return aAllDocsInArchive
    End Function

Open in new window

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

579 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