HELP PLEASE: VBA List Folder size and File size, but not include subfolder

ChristPotter

New Member
Joined
Dec 2, 2012
Messages
3
example: i've "A" folder at "D:".. Floder "A" have folder "B", "C", "D", file a-z.. And folder "B", "C", and "D" have a lot of files at each folder.. Can i list just folder "B", "C", "D", and file a-z; without file in folder "B", "C" and "D"? I want name folder/file and the size.. THANKS>>>
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Code:
Sub ListFoldersAndInfo()
  Dim FSO As Object
  Dim Folder As Object
  Dim FolderName As String
  Dim R As Long
  Dim Rng As Range
  Dim SubFolder As Object
  Dim Wks As Worksheet
  Dim RootFolder
  Dim filePath As String
  Dim fd As Object

    Set fd = Application.FileDialog(msoFileDialogFolderPicker)
    With fd
        .Title = "Select Folder"
        If .Show = -1 Then       'User pressed action button
            DoEvents
            FolderName = .SelectedItems(1)
        Else
            Set fd = Nothing
            Exit Sub
        End If
    End With
    Set fd = Nothing


    If Right(FolderName, 1) <> "\" Then FolderName = FolderName & "\"
    Set Wks = Worksheets(1)
    Set Rng = Wks.Range("B2")
    Wks.UsedRange.Offset(1, 0).ClearContents
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    
      Set Folder = FSO.GetFolder(FolderName)
      R = 1
      Rng.Cells(R, 1) = Folder.Name
      Rng.Cells(R, 2) = Folder.Path
      Rng.Cells(R, 3) = Round(Folder.Size / 1024, 1) & " KB"
      
        For Each Folder In Folder.SubFolders
          R = R + 1
          Rng.Cells(R, 1) = Folder.Name
          Rng.Cells(R, 2) = Folder.Path
          Rng.Cells(R, 3) = Round(Folder.Size / 1024, 1) & " KB"

        Next Folder
        
    Set FSO = Nothing
End sub

Sub Filedir()
        Range("A7:A100").ClearContents
        Cells(7, 1).Value = "File Name"
        Cells(7, 2).Value = "DateCreated"
        Cells(7, 3).Value = "DateLastModified"
        Cells(7, 4).Value = "DateLastAccessed"
        Cells(7, 5).Value = "File size"
        fpath = "D:\test\"   '<<<<<<<<<< to be changed
        Call ShowFolderList(fpath, 8, 1)
End Sub
Sub ShowFolderList(fpath, arow, col)

        Dim fs, f, f1, s, sf
        Set fs = CreateObject("Scripting.FileSystemObject")
        Set f = fs.GetFolder(fpath)
        Set sf = f.SubFolders
        Set NFile = f.Files

        For Each pf1 In NFile
            If pf1.Name = "" Then Exit Sub
'            attr = pf1.Attributes
            Cells(arow, col) = pf1.Name
            Cells(arow, col + 1) = pf1.DateCreated
            Cells(arow, col + 2) = pf1.DateLastModified
            Cells(arow, col + 3) = pf1.DateLastAccessed
            Cells(arow, col + 4) = pf1.Size
            
            arow = arow + 1
        Next
    End Sub
 
Upvote 0
Code:
Sub ListFoldersAndInfo()   Dim FSO As Object   Dim Folder As Object   Dim FolderName As String   Dim R As Long   Dim Rng As Range   Dim SubFolder As Object   Dim Wks As Worksheet   Dim RootFolder   Dim filePath As String   Dim fd As Object      Set fd = Application.FileDialog(msoFileDialogFolderPicker)     With fd         .Title = "Select Folder"         If .Show = -1 Then       'User pressed action button             DoEvents             FolderName = .SelectedItems(1)         Else             Set fd = Nothing             Exit Sub         End If     End With     Set fd = Nothing       If Right(FolderName, 1) <> "\" Then FolderName = FolderName & "\"     Set Wks = Worksheets(1)     Set Rng = Wks.Range("B2")     Wks.UsedRange.Offset(1, 0).ClearContents          Set FSO = CreateObject("Scripting.FileSystemObject")            Set Folder = FSO.GetFolder(FolderName)       R = 1       Rng.Cells(R, 1) = Folder.Name       Rng.Cells(R, 2) = Folder.Path       Rng.Cells(R, 3) = Round(Folder.Size / 1024, 1) & " KB"                For Each Folder In Folder.SubFolders           R = R + 1           Rng.Cells(R, 1) = Folder.Name           Rng.Cells(R, 2) = Folder.Path           Rng.Cells(R, 3) = Round(Folder.Size / 1024, 1) & " KB"          Next Folder              Set FSO = Nothing End sub  Sub Filedir()         Range("A7:A100").ClearContents         Cells(7, 1).Value = "File Name"         Cells(7, 2).Value = "DateCreated"         Cells(7, 3).Value = "DateLastModified"         Cells(7, 4).Value = "DateLastAccessed"         Cells(7, 5).Value = "File size"         fpath = "D:\test\"   '<<<<<<<<<< to be changed         Call ShowFolderList(fpath, 8, 1) End Sub Sub ShowFolderList(fpath, arow, col)          Dim fs, f, f1, s, sf         Set fs = CreateObject("Scripting.FileSystemObject")         Set f = fs.GetFolder(fpath)         Set sf = f.SubFolders         Set NFile = f.Files          For Each pf1 In NFile             If pf1.Name = "" Then Exit Sub '            attr = pf1.Attributes             Cells(arow, col) = pf1.Name             Cells(arow, col + 1) = pf1.DateCreated             Cells(arow, col + 2) = pf1.DateLastModified             Cells(arow, col + 3) = pf1.DateLastAccessed             Cells(arow, col + 4) = pf1.Size                          arow = arow + 1         Next     End Sub
can't read folder.. only read file... :( but thanks..
 
Upvote 0
Sub ListFoldersAndInfo() - shows folders
Sub Filedir() - shows file in folder fpath
Sub ShowFolderList(fpath, arow, col) - is sub called from Filedir
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,876
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top