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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get most recent file added to a directory. 1

Status
Not open for further replies.

cmn2

Programmer
Mar 6, 2003
73
0
0
US
Hello,

I would like to get the name of the most recent file added to a network directory. If I understand correctly, a FileSystemWatcher is more of a listener and wouldn't be appropriate in this case. I rather have an app that can be run at anytime.

Has anyone been tasked with this before and have some ideas or code they can lend? Any help would be greatly appreciated.

Thank you
 
Something like this should do it:

Code:
Public Function GetLatestFile(ByVal Path As String) As String
    Dim dMaxDate = Date.MinValue
    Dim sMaxFile As String = String.Empty
    For Each sFile As String In IO.Directory.GetFiles(Path)
        Dim dFileWritten As Date = IO.File.GetLastWriteTime(sFile)
        If dFileWritten > dMaxDate Then
            sMaxFile = sFile
            dMaxDate = dFileWritten
        End If
    Next

    Return sMaxFile
End Function

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
So simple. Its perfect! Thanks Rick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top