• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

add filenames from folder to collection object

i need to add filenames from a particular folder to a collection object. Later I need to compare a filenames from a different folder to this collection object and add the names if filenames are not already in the collection object.

how could i possibly do that?

3 Solutions
Mike TomlinsonMiddle School Assistant TeacherCommented:
Option Explicit

Private Sub Command1_Click()
    On Error Resume Next ' ignore duplicate key error (file is already in collection)
    Dim filesA As Collection
    Dim filesB As Collection
    Dim file As Variant
    ' get the filenames from two different directories into two different collections
    Set filesA = getFilesInDirectory("C:\Documents and Settings\Michael\My Documents\Downloads\Test1", "*")
    Set filesB = getFilesInDirectory("C:\Documents and Settings\Michael\My Documents\Downloads\Test2", "*")
    ' add everything from collection B to collection A
    ' if file already exists then an error will be thrown
    ' the "On Error Resume Next" statement will take care
    ' of the error and move on to the next file
    For Each file In filesB
        filesA.Add file, file
    Next file
    ' display resulting set from collection A
    For Each file In filesA
        Debug.Print file
    Next file
End Sub

Private Function getFilesInDirectory(targetDirectory As String, filePattern As String) As Collection
    Dim fileCollection As Collection
    Dim curFile As String
    On Error GoTo noSuchDirectory
    Set fileCollection = New Collection
    ChDir targetDirectory
    On Error GoTo 0
    curFile = Dir(filePattern)
    Do Until curFile = ""
        fileCollection.Add curFile, curFile
        curFile = Dir()
    Set getFilesInDirectory = fileCollection
    Exit Function
    MsgBox "Invalid Directory: " & targetDirectory
    Set getFilesInDirectory = fileCollection
End Function

u'll have to use The FileSystemObject

add reference to MS Scripting Runtime Library
here's the code

Dim cl As Collection

Private Sub Command1_Click()
Dim fso As FileSystemObject, fldr As Folder
Dim fl As File

Set fso = CreateObject("scripting.filesystemobject")
Set fldr = fso.GetFolder("c:\Neeru")

For Each fl In fldr.Files
    If checkExistsinCollection(fl.Name) = False Then
        cl.Add fl.Name
    End If

End Sub

Public Function checkExistsinCollection(flnm As String) As Boolean 'Checks if the file name already exist in the collection
    For i = 0 To cl.Count - 1
        If cl(i).Item = flnm Then
            checkExistsinCollection = True
            Exit Function
        End If
End Function
A slight modification to anv's solution.
If you're going to use the "Microsoft Scripting Runtime" library you might as well use a Scripting.Dictionary object instead of a collection.
This has an inbuilt 'Exists' function.
PS: You'll need a reference in Project/References to "Microsoft Scripting Runtime" .

Private Sub Command1_Click()
    Dim cl As Scripting.Dictionary
    Dim fso As Scripting.FileSystemObject
    Dim fldr As Scripting.Folder
    Dim fl As Scripting.File
    Set fso = New Scripting.FileSystemObject
    Set fldr = fso.GetFolder("c:\Neeru")
    Set cl = New Scripting.Dictionary
    For Each fl In fldr.Files
        If Not cl.Exists(fl.Name) Then
            cl.Add fl.Name, fl.Name
        End If
    Dim i As Long
    For i = 0 To cl.Count - 1
        Debug.Print cl.Items(i)
    Next i

End Sub

Hello itc307,
Accept my Greetings!!

The method Idle_Mind has proposed seems the best method in terms of speed as less objects are involved.  FileSystemObject may be somewhat slow in these operations.  I appreciate the On Error method used for discreminating the duplicate file names by using them as Keys in the collection.  I was about to propose a solution on the same lines but after Idle_minds code it will be just a repetation of the same idea.

I will add that using a wrapper class for these file collections will be more elegant and make the code maintainable and easy to understand.


Tushar Joshi
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now