• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

looping over subfolders

tomas

Active Member
Hi

I am trying to collect details about all files from parent folder and I know how to collect information about files in subfolder one level down. But each subfolder might contain another subfolder etc and I don't know how to get around it.
Code:
Option Explicit

Dim fso As Scripting.FileSystemObject
Dim folderpath As String
Dim fil As Scripting.File
Dim Klientfolder As Scripting.Folder
Dim subfolder As Scripting.Folder



Sub showsdics()

ThisWorkbook.Sheets("sheet1").Range("a2").Select

folderpath = "S:\KLIENTI"
Set fso = New Scripting.FileSystemObject

If fso.FolderExists(folderpath) Then
    Set Klientfolder = fso.GetFolder(folderpath)
End If

For Each fil In Klientfolder.Files
    ActiveCell.Value = fil.Name
    ActiveCell.Offset(0, 1).Value = fil.DateCreated
    ActiveCell.Offset(0, 2).Value = fil.Size
    ActiveCell.Offset(0, 3).Value = fil.DateLastModified
    ActiveCell.Offset(0, 4).Value = fil.ParentFolder
   
   
    ActiveCell.Offset(1, 0).Select
   
Next

For Each subfolder In Klientfolder.SubFolders


    For Each fil In subfolder.Files
    ActiveCell.Value = fil.Name
    ActiveCell.Offset(0, 1).Value = fil.DateCreated
    ActiveCell.Offset(0, 2).Value = fil.Size
    ActiveCell.Offset(0, 3).Value = fil.DateLastModified
    ActiveCell.Offset(0, 4).Value = fil.ParentFolder
    ActiveCell.Offset(1, 0).Select
   
Next

Next
End Sub

Also one more question if I want to collect information about all files from a particular disc I suppose I can't refer to it as scripting folder so what is the way to do it ?

Thank you
 
Back
Top