+ Reply to Thread
Results 1 to 9 of 9

List all files in directory in an Excel file

  1. #1
    Valued Forum Contributor
    Join Date
    03-23-2008
    Location
    Tennessee
    MS-Off Ver
    Office 2007
    Posts
    706

    List all files in directory in an Excel file

    Good morning, Gurus.

    In my search I found several example similar to what I need, but nothing I could adapt to filt my needs, (at least not wim my limited skills).

    Here what I need to do:
    I have multilple Excel files in a directory. (M:/Archived PO Responses/Domestic). On a daily basis, these files are processed via VBA, and deleted after processing. What I need to do is, prior to processing and deleting these files, create an ongoing log of the filenames in that directory.

    Example:
    The macro would open an Excel file named "Processed Orders.xls", which is stored on the network drive "M:". It would then append all of the filenames in the directory mentioned above to a sheet named "Processed Orders" in that workbook, below any filenames that already exist.

    Thanks in advance for any help you can offer.
    Last edited by [email protected]; 04-03-2009 at 11:47 AM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: List all files in directory in an Excel file

    Hello Matt,

    Here is a macro I wrote a little while ago because I needed to keep better track of my files. You can select the file type to list and do wild card matching on the file names. Copy this code into a standard module in the workbook "Processed Orders.xls". Add a button to the worksheet "Processed Orders" and attach the macro "ListFiles" to it. Here is the code...
    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Valued Forum Contributor
    Join Date
    03-23-2008
    Location
    Tennessee
    MS-Off Ver
    Office 2007
    Posts
    706

    Re: List all files in directory in an Excel file

    Thanks for the reply, Leith. I hope all is going well.

    I'll test your script as soon as I get to the office in the morning, and will let you know how it works out.

    Have a good one!

    Hutch

  4. #4
    Valued Forum Contributor
    Join Date
    03-23-2008
    Location
    Tennessee
    MS-Off Ver
    Office 2007
    Posts
    706

    Re: List all files in directory in an Excel file

    Good morning, Leith.

    The file seems to work great, with one small problem. It appears to be copying all BUT one of the filenames to the file. I've modified it to use on two different directories, "Domestic" and "Overseas", and i both instances there is one more file counted in the folder in "My Computer" than actually get copied to the worksheet. (I'm only counting the Excel files, too.)

    I was curious if ther was an element to the script that may keep it from copying ALL of the files.

    Thanks for all your help.

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: List all files in directory in an Excel file

    Hello Hutch,

    I'll have to double check the macro. I don't see anything obvious in the code and thought it listed all the files in the tests. If the same result is happening on 2 directories then something is not right. I'll look into it.

  6. #6
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: List all files in directory in an Excel file

    I tweaked Leith's code to insert all the found files. It was just a matter of moving 2 lines of code in the Do Loop.
    Please Login or Register  to view this content.

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: List all files in directory in an Excel file

    Hello Hutch,

    Found the problem. Using arrays can be confusing. Sometimes they are zero based, and other times one based.
    Please Login or Register  to view this content.

  8. #8
    Valued Forum Contributor
    Join Date
    03-23-2008
    Location
    Tennessee
    MS-Off Ver
    Office 2007
    Posts
    706

    Re: List all files in directory in an Excel file

    That fixed her right up. Thanks, guys!!

    Have a great weekend!

  9. #9
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: List all files in directory in an Excel file

    Here is another method just for kicks. It returns the full name. It uses the dictionary method so you would have to add the reference as I commented in the code.

    Of course if you ever need to get the subfolder files too, other methods would be needed. FileSearch is the easiest but Excel2007 does not include it so other alternatives are needed. Of course FileSearch is easily used for subfolders and single folder methods like this as well.

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1