finding newest timestamped file

steve_bris

Registered User.
Local time
Today, 11:19
Joined
Mar 22, 2005
Messages
30
Hi.

I need to write some code in access VBA to find the newest timestamped excel file in a directory....( say the file is called myname(date)(time).xls)..... and then make a copy of it and rename it to say yourname.xls.

Thanks for any help

Steve
 
I like the Microsoft Scripting Runtime for this kind of job. Set a reference to it in the Tools->References menu of a code window. Check out various properties and methods in the object browser, or create a File System Object...

Dim fso As New Scripting.FileSystemObject

... and use intellisense to explore its handy list of whiz-bang properties and methods.
 
Yet another reason to name your repetitive files using the YYMMDD format. It is easy to sort.
I've never used the time in a file name, but YYMMDDTTTT will do nicely.
 
Thanks Guys :)

So I only need to find the newest timestamp files now.......don'thave to worry about copying and stuff .

The name is like .... myname(23-02-2005 9 28 22 AM).xls

Any idea of how I would code it....I am very new to access :)
 
Using MS Scripting Runtime to find newest file

This code by-passes filenames and finds the newest file in the folder "folderName" by its "DateCreated" attribute in Windows. It's not exactly what you asked for, but I wanted to see if it could be done. Hope you can use it.

Code:
Function GetNewestFilename(folderName As String) As String
   Dim fso As New Scripting.FileSystemObject
   Dim fd As Scripting.Folder
   Dim f_loop As Scripting.File
   Dim f_tmp As Scripting.File
   
   [COLOR=Green]'use the fso to return a folder reference using PARAM folderName[/COLOR]
   Set fd = fso.GetFolder(folderName)
   
   [COLOR=Green]'enumerate the files collection of the folder object[/COLOR]
   For Each f_loop In fd.Files
      If f_tmp Is Nothing Then
         [COLOR=Green]'on the first iteration of this loop, f_tmp will not be assigned[/COLOR]
         Set f_tmp = f_loop [COLOR=Green]'so do this here with the first file[/COLOR]
      Else
         [COLOR=Green]'on subsequent iterations, see if the current f_loop is newer[/COLOR]
         If f_loop.DateCreated > f_tmp.DateCreated Then
            [COLOR=Green]'and if so, assign to f_tmp[/COLOR]
            Set f_tmp = f_loop
         End If
      End If
   Next f_loop
   
   GetNewestFilename = f_tmp.Path
      
End Function
 
As Lagbolt said, use the Scripting.FileSystemObject.

It occurs to me, though, that the time stamp relates to when it was created. If this is the case, you could loop through them using two FileSystemObject file variables and comparing the two's "datecreated" property as you loop through. Here is a function I just knocked up to demonstrate: (Replace "Drive:\Folder" accordingly, which could be fed to the function as a string. You could also copy the file as another name in this function, but I'll leave that fun for you.

Code:
Function FindNewestFile() As String 'Or as whatever you want
    Dim objFS As New Scripting.FileSystemObject
    Dim objFldr As Folder
    Dim objFile As File
    Dim objFile2 As File
    Set objFldr = objFS.GetFolder("Drive:\Folder")
    On Error Resume Next
    For Each objFile In objFldr.Files
        If Not x = True Then
            Set objFile2 = objFile
            x = True
        Else
            If objFile.DateCreated > objFile2.DateCreated Then
                Set objFile2 = objFile
            End If
        End If

    Next objFile
    FindNewestFile = objFile2.Name
    Set objFldr = Nothing
    Set objFile = Nothing
    Set objFile2 = Nothing
    Set objFS = Nothing

End Function
edit: OK, it looks like we had the same idea!
 
Last edited:
Sarge...

That's a trip eh? :eek: We wrote the same code!
Cheers man,
Mark
 
FYI

VBA has the FileDateTime() function that will return the date and time a file was created or last modified.

Code:
FileDateTime("pathname")
The Scripting.FileSystemObject is a powerful tool when working with files.
 
ghudson said:
FYI

VBA has the FileDateTime() function that will return the date and time a file was created or last modified.

Code:
FileDateTime("pathname")
The Scripting.FileSystemObject is a powerful tool when working with files.

Never heard of it. Guess that separates the gurus from the guys that do it "the hard way".

I learn something every day on this forum! Usually multiple things!
I love this place!!!

Sarge.
 
Same same. Learning is more fun than knowing. Knowing is what you're left with when the shine wears off your learning.
I just made that up.
Later,
Mark
 
lagbolt said:
Knowing is what you're left with when the shine wears off your learning.
Dude, I feel certain you're gonna be in a book of quotations some day. (Maybe not for THAT quote, but you show promise!)

Just kidding, I really do like your spur-of-the-moment quote.

Sarge.
 

Users who are viewing this thread

Back
Top Bottom