Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get modified date on files in folder

Status
Not open for further replies.

jammerdk

Technical User
Aug 16, 2001
51
DK
Hi guys

I'm trying to return the latest modified date of the all files in a folder.

Have tried this code

Dim Dato As String
Dato = DLookup("Path", "tblSys", "")
Me.txtMod.Value = FileDateTime(left((Dato), Len(Dato) - 1))

this returns the modified path modified date but I'd like the neweste modification date of all the files

Any ideas ???

 
You can set a reference to the Scripting library, and manipulate the FileSystemObject to retrieve the DateLastModified property of any or all folders/files in VBA.

An làmb a bheir, ‘s i a gheibh.
 
Here's a function you can use to get the last modified date of a file. You don't need to set a reference:

Code:
Function GetLastModDate(strFilePathAndName As String) As Date
    Dim fso As Object
    Dim f As Object
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    Set f = fso.GetFile(strFilePathAndName)
    GetLastModDate = f.DateLastModified
    

    Set fso = Nothing
End Function

You just call it like

MsgBox GetLastModDate("C:\Temp\MyFile.xls")

and there you go.

Bob Larson
Free Access Tutorials and Samples:
 
Thanx bob
Your code works great on 1 specific file in a path, but I'd it to look thru all the files in the path.

 
Have a look at the For Each...Next construct in Access Help.

An làmb a bheir, ‘s i a gheibh.
 
So jammerdk -

What do you want as output? How are you going to be using this information? If you provide the answers to those questions I might have an idea on how to modify the code appropriately.

Bob Larson
Free Access Tutorials and Samples:
 
Here is some of my code that I copied & pasted.
You will need to set the lib reference for this, but you may be able to modify it to suit your needs:
Code:
Dim fso As Scripting.FileSystemObject
Dim fol As Scripting.Folder
Dim fdr As Scripting.Folder
Dim fil As Scripting.File
Dim flc As Scripting.Folders

Set fso = CreateObject("Scripting.FileSystemObject")
Set fol = fso.GetFolder("YourPathName")
Set flc = fol.SubFolders

For Each fdr In flc

  For Each fil In fdr.Files
		Debug.Print fil.DateLastModified
  Next fil

Next fdr

Set fso = Nothing
Set fol = Nothing
Set flc = Nothing

An làmb a bheir, ‘s i a gheibh.
 
genomen:

If you make a small change you DON'T need to set a reference.
Change this:
Code:
Dim fso As Scripting.FileSystemObject
Dim fol As Scripting.Folder
Dim fdr As Scripting.Folder
Dim fil As Scripting.File
Dim flc As Scripting.Folders

to this:
Code:
Dim fso As Object
Dim fol As Object
Dim fdr As Object
Dim fil As Object
Dim flc As Object

No other changes necessary to make it run without references.

Bob Larson
Free Access Tutorials and Samples:
 
Good point! For some reason I was locked into early-binding objects at a young age. Used to save overhead "back in the day". Don't think it's much of an issue anymore, although let's not turn this thread into a discussion of early vs. late.
[wink]


An làmb a bheir, ‘s i a gheibh.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top