Solved

How do I loop thru a folder and retrieve the newest file

Posted on 2014-12-09
3
197 Views
Last Modified: 2014-12-11
Within VBA I want to loop thru a folder call Volunteer Daily and retrieve the latest newest file within the folder called Cash Donations ?
0
Comment
Question by:upobDaPlaya
3 Comments
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 450 total points
Comment Utility
try this codes

Function NewestFile(Directory, FileExt)
Dim FileName As String
Dim LatestFile As String
Dim LatestDate As Date
    If Right(Directory, 1) <> "\" Then Directory = Directory & "\"
    FileName = Dir(Directory & FileExt, 0)
    If FileName <> "" Then
        LatestFile = FileName
        LatestDate = FileDateTime(Directory & FileName)
        Do While FileName <> ""
            If FileDateTime(Directory & FileName) > LatestDate Then
                 LatestFile = FileName
                 LatestDate = FileDateTime(Directory & FileName)
             End If
             FileName = Dir
        Loop
    End If
    NewestFile = LatestFile
End Function
0
 
LVL 26

Assisted Solution

by:Nick67
Nick67 earned 50 total points
Comment Utility
retrieve the latest newest file
What is meant by retrieve
Open? Copy? Move? Get a string of the file's name for use in code?
Rey's code asks you to pass in a Path and a file extension.
It will return a string with just the filename & extension of the most newly modified file.
It does this by checking the date modified property of each and every file in the path you supplied and saving the name if it was modified later than the previously saved one.
When the loop is complete, the last modified file has been identified.
0
 

Author Closing Comment

by:upobDaPlaya
Comment Utility
The DIR function is exactly what I needed...thx
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now