Results 1 to 4 of 4

Thread: Get Files From Directory

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2011
    Posts
    3

    Get Files From Directory

    Hi,
    All I want to do is get all files from a directory with a given file extension (in this case, .wav) and store the filenames in a collection. Here's what I have. I get an error for the first line in Form_Load(), "Compile error: Argument not optional", and it highlights "filenames =". Anyone know what could be wrong?
    Cheers

    Option Explicit
    Dim filenames as New collection
    -----------------------------------------------------------------
    Private Sub Form_Load()

    filenames = GetFiles("E:\Documents and Settings\User1\My Documents\User2\VCV\stim", "*.wav")

    For c = 0 To Len(files)
    Debug.Print files(c); " ";
    Next c

    End Sub
    -----------------------------------------------------------------

    ' Returns a collection holding all the filenames that
    ' match a given filespec and search attributes.
    Function GetFiles(dirname As String, filespec As String) As collection

    Dim filename As String
    Set GetFiles = New collection

    ' start the search
    filename = Dir$(dirname & filespec, vbNormal)

    Do While Len(filename)
    ' we've found a new file
    GetFiles.Add filename, filename
    ' get ready for the next iteration
    filename = Dir$
    Loop

    End Function

  2. #2
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Get Files From Directory

    I dont use Collections but it looks like you are trying to use it as a simple variable. You need something like this:

    filenames.Add GetFiles("E:\Documents and Settings\User1\My Documents\User2\VCV\stim", "*.wav")

    Also,

    Code:
    For c = 0 To Len(files)
    Debug.Print files(c); " ";
    Next c
    perhaps should be:

    Code:
    For c = 0 To filenames.Count
      Debug.Print filenames.Item(c); " "; ' Not sure about ; " ";
    Next c
    Also, you are using Option Explicit so you need to define c



    Why not just use the File control; filter it for .wav only and bingo you're all set to go. It does all the work for you.
    Last edited by jmsrickland; May 16th, 2011 at 03:11 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2011
    Posts
    3

    Re: Get Files From Directory

    Thanks jmsrickland. I'll have a look into the File control.

    I seem to have resolved the problem by using Set to assign filenames. i.e.

    Set filenames = GetFiles("E:\Documents and Settings\User1\My Documents\User2\VCV\stim", "*.wav")

    Jeff

  4. #4
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Get Files From Directory

    Collection objects are nice when you want to store data of different data types but when it comes to just plain o'text the Listbox Control and the FileList Control are really nice to have.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width