Count file and modfication dates

rawr19911

Board Regular
Joined
Jan 21, 2020
Messages
88
Office Version
  1. 2016
i was needing something that would count file's list their names then their path then the modification date- however; the VBA i am looking at does not work it is failing at the first line and saying compile error: user defined type not defined was hoping someone could help or if they have another code that could work

Sub ListFilesinFolder()
Dim FSO As Scripting.FileSystemObject
Dim SourceFolder As Scripting.Folder
Dim FileItem As Scripting.File
SourceFolderName = "C:\Users\Path"
Set FSO = New Scripting.FileSystemObject
Set SourceFolder = FSO.GetFolder(SourceFolderName)
Range("A1:C1") = Array("text file", "path", "Date Last Modified")
i = 2
For Each FileItem In SourceFolder.Files
Cells(i, 1) = FileItem.Name
Cells(i, 2) = FileItem
Cells(i, 3) = FileItem.DateLastModified
i = i + 1
Next FileItem
Set FSO = Nothing
End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
saying compile error: user defined type not defined was hoping someone could help or if they have another code that could work

You have to add a reference to the Microsoft Scripting Runtime library to be able to reference the FileSystemObject in that way (early binding).

1693239300229.png
 
Upvote 0
Solution
You can also use late binding, which does not require an explicit library reference (not tested).

VBA Code:
Sub ListFilesinFolderLateBinding()
    Dim FSO As Object, SourceFolder As Object, FileItem As Object
    Dim SourceFolderName As String, i As Long 'don't forget to declare all variables
    
    SourceFolderName = "C:\Users\Path"
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    
    If FSO.FolderExists(SourceFolderName) Then
        Set SourceFolder = FSO.GetFolder(SourceFolderName)
        Range("A1:C1") = Array("text file", "path", "Date Last Modified")
        
        i = 2
        For Each FileItem In SourceFolder.Files
            Cells(i, 1) = FileItem.Name
            Cells(i, 2) = FileItem
            Cells(i, 3) = FileItem.DateLastModified
            i = i + 1
        Next FileItem
    Else
        MsgBox "Folder '" & SourceFolderName & "' not found"
    End If
    Set FSO = Nothing
End Sub
 
Upvote 1
You can also use late binding, which does not require an explicit library reference (not tested).

VBA Code:
Sub ListFilesinFolderLateBinding()
    Dim FSO As Object, SourceFolder As Object, FileItem As Object
    Dim SourceFolderName As String, i As Long 'don't forget to declare all variables
   
    SourceFolderName = "C:\Users\Path"
   
    Set FSO = CreateObject("Scripting.FileSystemObject")
   
    If FSO.FolderExists(SourceFolderName) Then
        Set SourceFolder = FSO.GetFolder(SourceFolderName)
        Range("A1:C1") = Array("text file", "path", "Date Last Modified")
       
        i = 2
        For Each FileItem In SourceFolder.Files
            Cells(i, 1) = FileItem.Name
            Cells(i, 2) = FileItem
            Cells(i, 3) = FileItem.DateLastModified
            i = i + 1
        Next FileItem
    Else
        MsgBox "Folder '" & SourceFolderName & "' not found"
    End If
    Set FSO = Nothing
End Sub
thanks- i have uploaded a new question similar as well- maybe you can help with that one ? you seem to really know your stuff!
 
Upvote 0

Forum statistics

Threads
1,215,076
Messages
6,122,983
Members
449,092
Latest member
Mr Hughes

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