Return Date Modified for File in Directory

Mitch3

New Member
Joined
Apr 2, 2013
Messages
3
How can I return the path, file name, and date modified if a file exist in a specific location? Is it possible with a macro?
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Code:
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 = "E:\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
with suggested function
Code:
Sub FiledirDate()
    Range("A2:A100").ClearContents
    Cells(2, 1).Value = "File Name"
    Cells(2, 2).Value = "Date"
    arow = 3
    fpath = "E:\prova\"   '<<<<<<<<<< to be changed
    fName = Dir(fpath & "*.xls*")
    Do While fName <> ""
      Cells(arow, 1) = fName
      Cells(arow, 2) = FileDateTime(fpath & fName)
      arow = arow + 1
      fName = Dir
    Loop
End Sub
 
Upvote 0
Incredible! Thank you Both!

Patel45, that is exactly what I needed. Thank you / Grazie Mille!
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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