PDA

View Full Version : Latest file name in the folder path



naveencn023
04-10-2014, 09:24 PM
Hi,

I need a macro for the below condition.

I have the folder paths in column A and i want to get the latest file created in that folder with its name in column B.

Thanks in advance for your help.

pike
04-14-2014, 04:13 AM
hi naveencn023 (http://www.vbaexpress.com/forum/member.php?53467-naveencn023)
can you adjust this code to suit?

Sub My_Newest_Files()
Dim objFSO As Object
Dim objFile As Object
Dim mfile As String
Dim mpath As String
Set objFSO = CreateObject("Scripting.FileSystemObject")
mpath = "C:\Documents and Settings\"
mfile = Dir(mpath & "*.*")
Do While mfile <> ""
mfile = Dir
If objFSO.FileExists(mpath & mfile) Then
Set objFile = objFSO.GetFile(mpath & mfile)
If objFile.DateLastModified > Cells(1, 1).Value Then
Cells(1, 1).Value = objFile.DateLastModified
Cells(1, 2).Value = mfile
End If
End If
Loop
End Sub