Solved

add filenames from folder to collection object

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

831 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