Wednesday, October 21, 2009

Find the latest File name in a Folder

We can find the lastest file name(Based on file creation date) in specified folder with below code.
In some applications at runtime files are creating and storing in share point, but we do not have control on file name and have only read only access. In these scenarios below code is helpful for latest file name capture.
Code:-
sPath=" C:\"
GetNewestFile sPath

' getting file name

Function GetNewestFile (byval sPath)
sNewestFile = Null ' init value
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder =objFSO.GetFolder(sPath)
Set oFiles = oFolder.Files
For Each oFile In oFiles
On Error Resume Next
If IsNull(sNewestFile) Then
sNewestFile = oFile.Path
Datatable.Value("FilePath","Global") =sNewestFile
msgbox sNewestFile
dPrevDate = oFile.DateLastModified
msgbox dPrevDate
Elseif dPrevDate < oFile.DateLastModified Then
sNewestFile = oFile.Path
msgbox sNewestFile
Datatable.Value("FilePath","Action1") =sNewestFile
End If
On Error Goto 0
Next
If IsNull(sNewestFile) Then
sNewestFile = ""
GetNewestFile = sNewestFile
msgbox GetNewestFile
End If
End Function

No comments:

Post a Comment