Hi All,
I m having a folder which consist of multiple files and i want the name of the file ie last accesd by any one means i need to know which file has updated most recently.
regards and thanks
Hi All,
I m having a folder which consist of multiple files and i want the name of the file ie last accesd by any one means i need to know which file has updated most recently.
regards and thanks
Hi
Try
and call the function likeCode:Function GetLastModifiedFile(ByVal FolderName As String, Optional FileType As String = "Excel") As String Dim objFso As Object Dim Fldr As Object Dim f, FileName As String Dim d As Single Dim t As Single, e As String Set objFso = CreateObject("Scripting.FileSystemObject") If Right$(FolderName, 1) <> Application.PathSeparator Then FolderName = FolderName & Application.PathSeparator Set Fldr = objFso.getfolder(FolderName) d = 0 For Each f In Fldr.Files FileName = f.Name e = Mid$(FileName, InStrRev(FileName, ".") + 1) Select Case UCase$(FileType) Case "EXCEL" If e Like "xl*" Then t = f.datelastmodified If t > d Then d = t: GetLastModifiedFile = FileName End If End If Case "WORD" If e Like "do*" Then t = f.datelastmodified If t > d Then d = t: GetLastModifiedFile = FileName End If End If Case "POWERPOINT" If e Like "pp*" Then t = f.datelastmodified If t > d Then d = t: GetLastModifiedFile = FileName End If End If End Select Next End Function
Code:Sub kTest() MsgBox GetLastModifiedFile("YourFolder") End Sub
Cheers !
Excel Range to BBCode Table
Use Social Networking Tools If You Like the Answers !
Message to Cross Posters
@ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)
You could also write Admin's function using VB's built in FileDateTime function instead of calling out to the FileSystemObject scripting object (note that I reorganized the code in keeping with my own personal programming preferences)...
Code:Function GetLastModifiedFile(ByVal FolderName As String, Optional FileType As String = "Excel") As String Dim FN As String, FileName As String, FileNamePattern As String Dim NewestFileName As String, MaxDate As Date If Right$(FolderName,1) <> Application.PathSeparator Then FolderName = FolderName & Application.PathSeparator Select Case UCase(FileType) Case "EXCEL": FileNamePattern = "*.xl*" Case "WORD": FileNamePattern = "*.do*" Case "POWERPOINT": FileNamePattern = "*.pp*" End Select FN = Dir$(FolderName & FileNamePattern) Do While FN <> "" If FileDateTime(FolderName & FN) > MaxDate Then MaxDate = FileDateTime(FolderName & FN) NewestFileName = FN End If FN = Dir$ Loop GetLastModifiedFile = NewestFileName End Function
Last edited by Rick Rothstein; 04-11-2012 at 10:23 AM.
Hi Admin and Rick thanks alot for ur quick reply
Bookmarks