VBA code to generate a list of files and sizes in subfolders

classmaz

New Member
Joined
Apr 25, 2017
Messages
11
Hello, I have found code generated in this forum to generate a list of files in a specific folder and all its subfolders that have been modified in the past 100 days. However, when running it on the network drive it generates an error and stops. I think the error is caused by permission issues and I would like to exclude the subfolders I cant open. Would I have to specify them, or can the code just skip folders I don't have access to see? any help appreciated. Below is the code i am trying to use

VBA Code:
Sub getfiles()

    Dim oFSO As Object
    Dim oFolder As Object
    Dim oFile As Object, sf
    Dim i As Integer, colFolders As New Collection, ws As Worksheet
   
    Set ws = ActiveSheet
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oFolder = oFSO.getfolder("F:\")
   

    colFolders.Add oFolder          'start with this folder
   
    Do While colFolders.Count > 0      'process all folders
        Set oFolder = colFolders(1)    'get a folder to process
        colFolders.Remove 1            'remove item at index 1
   
        For Each oFile In oFolder.Files
            If oFile.DateLastModified > Now - 100 Then
                ws.Cells(i + 1, 1) = oFolder.Path
                ws.Cells(i + 1, 2) = oFile.Name
                ws.Cells(i + 1, 3) = "RO"
                ws.Cells(i + 1, 4) = oFile.DateLastModified
                ws.Cells(i + 1, 5) = oFile.Size / 1024 ^ 2
                i = i + 1
            End If
        Next oFile

        'add any subfolders to the collection for processing
        For Each sf In oFolder.subfolders
            colFolders.Add sf 'add to collection for processing
        Next sf
    Loop

End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
If the macro works for you and the issue is permissions, then after this line:
VBA Code:
colFolders.Add oFolder          'start with this folder

Put this line:
VBA Code:
On Error Resume Next
 
Upvote 0
If the macro works for you and the issue is permissions, then after this line:
VBA Code:
colFolders.Add oFolder          'start with this folder

Put this line:
VBA Code:
On Error Resume Next
I would like to log the files/sub folders that generate an error on another tab. Can you assist?

I have just ran the code with the date modified filter of
VBA Code:
oFile.DateLastModified < Now + 1
so It would list all the files in the folder and sub folder and this could be compared against right clicking the folder and doing properties. Unfortunately, the validation highlighted a difference of 13 000 (20gb) files short. Any assistance would be appreciated.
 
Upvote 0
Power Query alternative: From File --> From Folder. Add a custom "cutoff" column a hundred days before today with

Power Query:
Date.AddDays(Date.From(DateTime.LocalNow()),-100)

Then a conditional column that compares the cutoff to the Date modified column, and then a filter.
 
Upvote 0
Power Query alternative: From File --> From Folder. Add a custom "cutoff" column a hundred days before today with

Power Query:
Date.AddDays(Date.From(DateTime.LocalNow()),-100)

Then a conditional column that compares the cutoff to the Date modified column, and then a filter.
I tried power query and if failed to retrieve all the data as I don't have permissions for some of the subfolders. Any tips for this?
 
Upvote 0
If the macro works for you and the issue is permissions, then after this line:
VBA Code:
colFolders.Add oFolder          'start with this folder

Put this line:
VBA Code:
On Error Resume Next
So I have been left confused the original code works for the first 3 subfolders and all their subfolders. It then stops doing the sub-subfolders, I am not sure if it is the on error resume next code? any help?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

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