?
Solved

add filenames from folder to collection object

Posted on 2004-09-01
7
Medium Priority
?
233 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 672 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 664 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 664 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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 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…
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…
Suggested Courses

764 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