Solved

add filenames from folder to collection object

Posted on 2004-09-01
7
215 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

747 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

10 Experts available now in Live!

Get 1:1 Help Now