+ Reply to Thread
Results 1 to 7 of 7

dir all files in a folder and sub folders

Hybrid View

  1. #1
    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: dir all files in a folder and sub folders

    Hello Pero,

    You didn't do anything wrong. Any Sub or Function that takes arguments is automatically hidden from the macro list by Excel. Here is a macro that will be visible. Change the path to the one you want.
    Sub Macro1()
      ListFilesInFolder "C:\Documents and Settings\Admin.Admins\My Documents", True
    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!)

  2. #2
    Registered User
    Join Date
    08-20-2009
    Location
    Stockholm,Sweden
    MS-Off Ver
    Excel 2003
    Posts
    43

    Re: dir all files in a folder and sub folders

    Thanks again for the help !

    The macro is now visible in the macro list but now when i try to run the macro it says "Sub or function could not be defined"
    And the text marked yellow text is:

    Sub ListFilesInFolder(ByVal SourceFolderName As String, ByVal IncludeSubfolders As Boolean)
    I also want the macro to start at J13 to O13 instead of A:F.
    Also I want the filepath be be a variabel that is red from the column C3.

    Sry for my lack of knowlage in this matter, I'm trying to learn.

    Many many thanks !

    My code right now is:
      
    Sub makro1()
     
      ListFilesInFolder "C:\Users\Per\Desktop\Filer", True
    End Sub
    
    
    Sub ListFilesInFolder(ByVal SourceFolderName As String, ByVal IncludeSubfolders As Boolean)
    
      Dim FSO As Object
      Dim SourceFolder As Object
      Dim SubFolder As Object
      Dim FileItem As Object
      Dim R As Long
      
         Set FSO = CreateObject("Scripting.FileSystemObject")
         Set SourceFolder = FSO.GetFolder(SourceFolderName)
         
        'Find last row that has data
         R = Cells.Find("*", , xlValues, xlWhole, xlByRows, xlPrevious, False).Row
          
          'Display file properties in the next row in columns "A:F"
           For Each FileItem In SourceFolder.Files
             R = R + 1
             Cells(R, 1) = FileItem.Name
             Cells(R, 2) = FileItem.Path
             Cells(R, 3) = FileItem.Size
             Cells(R, 4) = FileItem.DateCreated
             Cells(R, 5) = FileItem.DateLastModified
             Cells(R, 6) = GetFileOwner(SourceFolder.Path, FileItem.Name)
           Next FileItem
           
           If IncludeSubfolders Then
             For Each SubFolder In SourceFolder.SubFolders
               ListFilesInFolder SubFolder.Path, True
             Next SubFolder
           End If
     
        Set FileItem = Nothing
        Set SourceFolder = Nothing
        Set FSO = Nothing
        
        ActiveWorkbook.Saved = True
        
    End Sub

    Thanks in advance !


    // Per
    Last edited by Pero; 09-09-2009 at 06:00 PM.

  3. #3
    Registered User
    Join Date
    09-19-2009
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: dir all files in a folder and sub folders

    Hi,

    I think the poster forgot to post the codes for the Get Owner macro.

    You can find it here:

    http://www.excelforum.com/excel-prog...in-folder.html

  4. #4
    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: dir all files in a folder and sub folders

    Hello Pero and Jubinell,

    Excellent catch on the missing macro Jubinell! I wrote the code and completely missed the obvious. Here is the full code with the corrections.

    Calling Macro
    Sub Macro1()
      ListFilesInFolder Range("C3"), True
    End Sub
    List Files In Folder Macro
    Sub ListFilesInFolder(ByVal SourceFolderName As String, ByVal IncludeSubfolders As Boolean)
    
      Dim FSO As Object
      Dim SourceFolder As Object
      Dim SubFolder As Object
      Dim FileItem As Object
      Dim R As Long
      
         Set FSO = CreateObject("Scripting.FileSystemObject")
         Set SourceFolder = FSO.GetFolder(SourceFolderName)
         
        'Find last row that has data
         R = Cells.Find("*", , xlValues, xlWhole, xlByRows, xlPrevious, False).Row
          
          'Display file properties in the next row in columns "J:O"
           For Each FileItem In SourceFolder.Files
             R = R + 1
             Cells(R, 10) = FileItem.Name
             Cells(R, 11) = FileItem.Path
             Cells(R, 12) = FileItem.Size
             Cells(R, 13) = FileItem.DateCreated
             Cells(R, 14) = FileItem.DateLastModified
             Cells(R, 15) = GetFileOwner(SourceFolder.Path, FileItem.Name)
           Next FileItem
           
           If IncludeSubfolders Then
             For Each SubFolder In SourceFolder.SubFolders
               ListFilesInFolder SubFolder.Path, True
             Next SubFolder
           End If
           
        Columns("J:O").AutoFit
      
        Set FileItem = Nothing
        Set SourceFolder = Nothing
        Set FSO = Nothing
        
        ActiveWorkbook.Saved = True
        
    End Sub
    Get File Owner Macro
    Function GetFileOwner(ByVal FilePath As String, ByVal FileName As String)
    
      Dim objFolder As Object
      Dim objFolderItem As Object
      Dim objShell As Object
    
        FileName = StrConv(FileName, vbUnicode)
        FilePath = StrConv(FilePath, vbUnicode)
    
         Set objShell = CreateObject("Shell.Application")
         Set objFolder = objShell.Namespace(StrConv(FilePath, vbFromUnicode))
         
           If Not objFolder Is Nothing Then
             Set objFolderItem = objFolder.ParseName(StrConv(FileName, vbFromUnicode))
           End If
           
           If Not objFolderItem Is Nothing Then
             GetFileOwner = objFolder.GetDetailsOf(objFolderItem, 8)
           Else
             GetFileOwner = ""
           End If
           
         Set objShell = Nothing
         Set objFolder = Nothing
         Set objFolderItem = Nothing
         
    End Function

+ 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