Solved

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

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
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.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
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 …

739 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