Solved

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

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need to filter query and have subform updatable 2 28
object variable or with block not set 6 31
Criteria for Date for DCount 4 26
aggregate query? 20 52
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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…

822 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