[AccessD] A2003: Get Latest File in a folder

Arthur Fuller fuller.artful at gmail.com
Sun Dec 9 23:13:01 CST 2007


How strange. I have code that does precisely this! It must be Christmas! No
promises that this code is optimal but it works.

<code>
'---------------------------------------------------------------------------------------
' Procedure : NewestFile
' DateTime  : 8/10/2007 12:04
' Author    : Arthur Fuller
' Purpose   :
' Returns the name of the most recent file in a Directory
' That matches the FileSpec (e.g., "*.xls").
' Returns an empty string if the directory does not exist or
' it contains no matching files
'---------------------------------------------------------------------------------------
'
Function NewestFile(Directory, FileSpec)
    Dim FileName As String
    Dim MostRecentFile As String
    Dim MostRecentDate As Date
   On Error GoTo NewestFile_Error

    If Right(Directory, 1) <> "\" Then Directory = Directory & "\"
    FileName = Dir(Directory & FileSpec, 0)
    If FileName <> "" Then
        MostRecentFile = FileName
        MostRecentDate = FileDateTime(Directory & FileName)
        Do While FileName <> ""
            If FileDateTime(Directory & FileName) > MostRecentDate Then
                 MostRecentFile = FileName
                 MostRecentDate = FileDateTime(Directory & FileName)
             End If
             FileName = Dir
        Loop
    End If
    NewestFile = MostRecentFile

   On Error GoTo 0
   Exit Function

NewestFile_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure
NewestFile of Module FileDateFolderStuff"
End Function
</code>

You'll probably have to rename a few things to work in your system, but this
code works. hth.

Arthur

P.S.
I want to comment on your "Hi Team" salutation. We are indeed a team! Should
any employer question my time occupied on this site, I simply respond that
she is hiring me + 2000 people who know what they're doing, all for one low
price. I value this place more than I can describe. Suffice to say,
"greatest space I have ever found."

A.

On 12/9/07, Darren D <darren at activebilling.com.au> wrote:
>
> Hi Team
>
>
>
> I need to determine the file name of the 'last' file in a given folder
>
> In this instance I need to use define the 'last' file in a folder by the
> date/time
>
>
>
> EG In a folder I may have 3 files
>
> 123XYZA012820071205
>
> 123XYZA012920071205
>
> 123XYZA013020071205
>
>
>
> Where 123XYZA013020071205 is the 'latest' file in the folder by say.a few
> hours
>
> Though the time diff is not important - just so long as I can tell it is
> the
> most recent file
>
>
>
> I appreciate the files have date stamps in their file name - this is not
> suitable - I need the latest by date and time
>
> I also can see the files have sequential numbers 128,129 and 130 in the 3
> I have
> provided
>
> This is the issue though I am trying to determine If the highest
> sequential no
> = the latest file
>
> This is to avoid loosing sequence etc with our suppliers
>
>
>
> Any pointers/suggestions?
>
>
>
> I assume I will be using the FSO object - I am familiar with that
>
> I also know it has the ability to interrogate the date/time attribs of
> files
>
> Though I am not sure how to loop through all the files in a folder and
> determine
> the newest one - that's the bit I am clueless on
>
> (Andy - No comments about how clueless I am in general :-) )
>
>
>
> NB: these files do not have extensions - So that can't be part of any
> suggested
> solution
>
>
>
> Many thanks in advance
>
>
>
> Darren
>
>
>
>
>
>
>
>
>
> --
> AccessD mailing list
> AccessD at databaseadvisors.com
> http://databaseadvisors.com/mailman/listinfo/accessd
> Website: http://www.databaseadvisors.com
>



More information about the AccessD mailing list