+ Reply to Thread
Results 1 to 9 of 9

List all files in directory in an Excel file

Hybrid View

  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 hutch@edge.net; 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,259

    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...
    'Written: February 19, 2009
    'Author:  Leith Ross
    'Summary: Creates a file list and copies it to the Active Sheet start at "A1".
    '         The arguments are the directory to be searched, the file type (extension),
    '         and optionally a wildcard filter for file names. Only the rquired argument
    '         is the directory path.
    
    Function CreateFileList(ByVal FolderPath As String, Optional ByVal FileType As String, Optional ByVal FileFilter As String) As Variant
    
      Dim Cnt As Long
      Dim FileList() As String
      Dim FileName As String
    
        On Error GoTo OutOfHere
        
        If Right(FolderPath, 1) <> "\" Then FolderPath = FolderPath & "\"
        If FileType = "" Then
           FileType = ".*"
        Else
           If Left(FileType, 1) <> "." Then FileType = "." & FileType
        End If
        If FileFilter = "" Then FileFilter = "*"
        
          FileName = Dir(FolderPath & FileFilter & FileType)
            Do While FileName <> ""
              FileName = Dir()
              ReDim Preserve FileList(Cnt)
              FileList(Cnt) = FileName
              Cnt = Cnt + 1
           Loop
          
    OutOfHere:
         If Err = 0 Then CreateFileList = FileList
         On Error GoTo 0
         
    End Function
    
    Sub ListFiles()
    
      Dim I As Long
      Dim MyFiles As Variant
      Dim MyArray() As String
      Dim N As Long
      Dim Rng As Range
      Dim RngEnd As Range
      
        MyFiles = CreateFileList("M:/Archived PO Responses/Domestic", ".xls")
        
          N = UBound(MyFiles)
          ReDim MyArray(N, 0)
          
            For I = 0 To N
              MyArray(I, 0) = MyFiles(I)
            Next I
            
            Set Rng = Worksheets("Processed Orders").Range("A1")
            Set Rng = Cells(Rows.Count, Rng.Column).End(xlUp).Offset(1, 0)
            Set Rng = Rng.Resize(N + 1, 1)
            
            Rng = MyArray
      
    End Sub
    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,259

    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.
    Sub Test_CreateFileList()
      Dim a
      a = CreateFileList("x:\Workbooks\")
      ActiveSheet.UsedRange.ClearContents
      Range("A1").Resize(UBound(a) + 1).value = WorksheetFunction.Transpose(a)
    End Sub
    
    
    Function CreateFileList(ByVal FolderPath As String, Optional ByVal FileType As String, Optional ByVal FileFilter As String) As Variant
    
      Dim Cnt As Long
      Dim FileList() As String
      Dim FileName As String
    
        On Error GoTo OutOfHere
        
        If Right(FolderPath, 1) <> "\" Then FolderPath = FolderPath & "\"
        If FileType = "" Then
           FileType = ".*"
        Else
           If Left(FileType, 1) <> "." Then FileType = "." & FileType
        End If
        If FileFilter = "" Then FileFilter = "*"
        
          FileName = Dir(FolderPath & FileFilter & FileType)
            Do While FileName <> ""
              ReDim Preserve FileList(Cnt)
              FileList(Cnt) = FileName
              FileName = Dir()
              Cnt = Cnt + 1
           Loop
          
    OutOfHere:
         If Err = 0 Then CreateFileList = FileList
         On Error GoTo 0
         
    End Function

  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,259

    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.
    'Written: February 19, 2009 (Updated April 03, 2009)
    'Author:  Leith Ross
    'Summary: Creates a file list and copies it to the Active Sheet start at "A1".
    '         The arguments are the directory to be searched, the file type (extension),
    '         and optionally a wildcard filter for file names. Only the rquired argument
    '         is the directory path.
    
    Function CreateFileList(ByVal FolderPath As String, Optional ByVal FileType As String, Optional ByVal FileFilter As String) As Variant
    
      Dim Cnt As Long
      Dim FileList() As String
      Dim FileName As String
    
        On Error GoTo OutOfHere
        
        If Right(FolderPath, 1) <> "\" Then FolderPath = FolderPath & "\"
        If FileType = "" Then
           FileType = ".*"
        Else
           If Left(FileType, 1) <> "." Then FileType = "." & FileType
        End If
        If FileFilter = "" Then FileFilter = "*"
        
          FileName = Dir(FolderPath & FileFilter & FileType)
            Do While FileName <> ""
              Cnt = Cnt + 1
              ReDim Preserve FileList(Cnt)
              FileList(Cnt) = FileName
              FileName = Dir()
           Loop
          
    OutOfHere:
         If Err = 0 Then CreateFileList = FileList
         On Error GoTo 0
         
    End Function
    
    Sub ListFiles()
    
      Dim I As Long
      Dim MyFiles As Variant
      Dim MyArray() As String
      Dim N As Long
      Dim Rng As Range
      Dim RngEnd As Range
      
        MyFiles = CreateFileList("C:\Documents and Settings\Admin.ADMINS\My Documents", ".txt")
        
          N = UBound(MyFiles)
          ReDim MyArray(N, 0)
          
            For I = 1 To N
              MyArray(I, 0) = MyFiles(I)
            Next I
            
            Set Rng = Worksheets("Sheet2").Range("A2")
            Set Rng = Cells(Rows.Count, Rng.Column).End(xlUp)
            Set Rng = Rng.Resize(N + 1, 1)
            
            Rng = MyArray
      
    End Sub

+ 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