Solved

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

Posted on 2014-12-09
3
209 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
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 how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

617 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