Visual Basic .NET - Determine if a file name is already in a FileInfo ArrayList

olinccu
olinccu used Ask the Experts™
on
I have an ArrayList of FileInfo objects.  Let's say I have a file object with a FullName of c:\temp\x1234.log and I want to know if a file with the same name is already in my ArrayList (but not necessarily with the same Full Name path...I ONLY want to know if it has the same file name).  Is there a way to do that other than something like this (see code), which requires me to loop through the ArrayList to compare every file I want to search for.  I'm hoping there's a less CPU intensive way to perform the task.
Dim c As Integer = DestinationFileList.Count
                For i As Integer = 0 To c - 1
                    If DestinationFileList.Item(i).Name.ToString = CurrentFileInfo.Name.ToString Then
                        If DestinationFileList.Item(i).LastWriteTime <= CurrentFileInfo.LastWriteTime Then
                            fileAlreadyExists = True
                        End If
                    End If
                Next

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I think the best thing to do here is to use a HashTable, which allows you to not only store your full filenames, but also associate each one with a key. For the key, you could let System.IO.Path.GetFileName() determine the actual filename, regardless of the path.

Author

Commented:
ToddBeauleiu:  That's a good idea.  I implemented HashTables instead and it cut the processing time from 10 minutes to 4.5 minutes....a much-welcomed improvement!  I'm open to more/other ideas to cut the processing time even more.  Thanks!
For further scrutiny, you should post the main chunk of code, where most of the time is spent.

Author

Commented:
This helped out a LOT.  Thank you for your help!  If I feel the need to optimize the program even more, I will post another question with more details/code in the question.  Thanks again for your help!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial