Results 1 to 2 of 2

Thread: RESOLVED[File listing script]

  1. #1

    Thread Starter
    Addicted Member Fonty's Avatar
    Join Date
    May 2006
    Location
    New York
    Posts
    173

    Resolved RESOLVED[File listing script]

    I'm doing a list of files and it's properties for some files contained in some subdirectories. For this I'm using a script I found here , for which I have only few options like these:

    VB Code:
    1. Sub ListFilesInFolder(SourceFolderName As String, IncludeSubfolders As Boolean)
    2. Dim SourceFolder As Scripting.Folder
    3. Dim SubFolder As Scripting.Folder
    4. Dim FileItem As Scripting.File
    5. Dim r As Long
    6.     Set FSO = New Scripting.FileSystemObject
    7.     Set SourceFolder = FSO.GetFolder(SourceFolderName)
    8.     r = Range("A65536").End(xlUp).Row + 1
    9. ' File properties:
    10.     For Each FileItem In SourceFolder.Files
    11.         Cells(r, 1).Formula = FileItem.ParentFolder
    12.         Cells(r, 2).Formula = FileItem.Name
    13.         Cells(r, 3).Formula = FileItem.Size
    14.         Cells(r, 4).Formula = FileItem.Type
    15.         Cells(r, 5).Formula = FileItem.DateCreated
    16.         Cells(r, 6).Formula = FileItem.DateLastAccessed
    17.         Cells(r, 7).Formula = FileItem.DateLastModified
    18.         Cells(r, 8).Formula = FileItem.Drive
    19.         r = r + 1
    20.     Next FileItem
    21.     If IncludeSubfolders Then
    22.         For Each SubFolder In SourceFolder.SubFolders
    23.             ListFilesInFolder SubFolder.Path, True
    24.         Next SubFolder
    25.     End If
    26.     Set FileItem = Nothing
    27.     Set SourceFolder = Nothing
    28.     Set FSO = Nothing
    29. End Sub

    What I want to add is the name of individual subfloders. For example, for 2 files:
    C:\folder\subfolder1\subfolder2\file1
    C:\folder\subfolder1\file2

    I'd like to have

    Name Folde1 Folder2 Folder3
    file1 folder subfolder1 subfolder2
    file2 folder subfolder1

    I'm able to do something like this using MID() and FIND() instructions in the sheet, however I'd like to automate it.
    Last edited by Fonty; Jun 13th, 2006 at 10:04 PM.

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: File listing script

    Try the following, it will add the folder details to the right of your exist data.
    Note: I have also replaced your .Formula properties with .Value - no need to use the .formula property if you are not writing a formula

    VB Code:
    1. Sub ListFilesInFolder(SourceFolderName As String, IncludeSubfolders As Boolean)
    2. Dim FSO As Scripting.FileSystemObject
    3. Dim SourceFolder As Scripting.Folder
    4. Dim SubFolder As Scripting.Folder
    5. Dim FileItem As Scripting.File
    6. Dim r As Long, x As Long
    7. Dim asFolders() As String
    8.  
    9.     Set FSO = New Scripting.FileSystemObject
    10.     Set SourceFolder = FSO.GetFolder(SourceFolderName)
    11.     r = Range("A65536").End(xlUp).Row + 1
    12. ' File properties:
    13.     For Each FileItem In SourceFolder.Files
    14.         Cells(r, 1).Value = FileItem.ParentFolder
    15.         Cells(r, 2).Value = FileItem.Name
    16.         Cells(r, 3).Value = FileItem.Size
    17.         Cells(r, 4).Value = FileItem.Type
    18.         Cells(r, 5).Value = FileItem.DateCreated
    19.         Cells(r, 6).Value = FileItem.DateLastAccessed
    20.         Cells(r, 7).Value = FileItem.DateLastModified
    21.         Cells(r, 8).Value = FileItem.Drive
    22.         asFolders = Split(FileItem.ParentFolder, "\")
    23.         For x = 1 To UBound(asFolders)
    24.             Cells(r, 8 + x).Value = asFolders(x)
    25.         Next x
    26.         r = r + 1
    27.     Next FileItem
    28.     If IncludeSubfolders Then
    29.         For Each SubFolder In SourceFolder.SubFolders
    30.             ListFilesInFolder SubFolder.Path, True
    31.         Next SubFolder
    32.     End If
    33.     Set FileItem = Nothing
    34.     Set SourceFolder = Nothing
    35.     Set FSO = Nothing
    36. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

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