Solved

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

Posted on 2014-12-09
3
203 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 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 450 total points
ID: 40490425
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
ID: 40494488
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
ID: 40495512
The DIR function is exactly what I needed...thx
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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.

685 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