Solved

add filenames from folder to collection object

Posted on 2004-09-01
7
216 Views
Last Modified: 2010-05-02
hi,
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?

thanks
sanjana
0
Comment
Question by:itc307
7 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 168 total points
ID: 11960414
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()
    Loop
    Set getFilesInDirectory = fileCollection
    Exit Function
   
noSuchDirectory:
    MsgBox "Invalid Directory: " & targetDirectory
    Set getFilesInDirectory = fileCollection
End Function

0
 
LVL 10

Assisted Solution

by:anv
anv earned 166 total points
ID: 11960434
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
Next


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
        Else
            checkExistsinCollection
        End If
    Next
End Function
0
 
LVL 18

Assisted Solution

by:JR2003
JR2003 earned 166 total points
ID: 11961061
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.
JR
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
    Next
   
    Dim i As Long
    For i = 0 To cl.Count - 1
        Debug.Print cl.Items(i)
    Next i

End Sub

0
 

Expert Comment

by:tusharvjoshi
ID: 11977070
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.

Regards

Tushar Joshi
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now