Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to open the most currently dated file within a folder

Status
Not open for further replies.

belstoy

Technical User
May 31, 2005
37
US
I am creating a job to open the most recently dated file within a specific folder. The files have a naming convention of "filename.yyyymmdd.xls"

Looking for code to aid me in doing this.

Thanks,
Belstoy
 
The File System Object in the Scripting Runtime library has methods for the file object to enable you to do this. Have a look in help and object browser; post back if you need help with the code.

Cogito eggo sum – I think, therefore I am a waffle.
 
Thanks.

I did some research, but I am still having an issue with the actual code.

I ran across some code, However, I am having an issue pointing it to the right folder.

Application.RecentFiles(2).Open
 
RecentFiles has nothing to do with FileSystemObject.

1. if you have specific questions, please ask a specific question.

2. if you have issue with specific code you have written, then post that code.

We are not mind readers, and we can not look over your shoulder and see your monitor.

If you want "to open the most recently dated file within a specific folder" - which is what you wrote - then using RecentFiles is likely not what you want to use. What if there is a file - recently opened - that is not from that folder?

genomon is correct. FSO is the route you want to go, at least "to open the most recently dated file within a specific folder".

There are lots of examples of the use of FSO, both here and other forums. Please post actual code if/when you have problems.
I ran across some code, However, I am having an issue pointing it to the right folder.
is simply too vague.


Just to clear though, RecentFiles is not "a specific folder". It is a list of....recent files, and they can be in any number of folders.

If that is what you want to do, fine, say so, and we can probably help.

But if it really is "a specific folder", then FSO is your best route.

"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
Thanks for your constructive criticism.

I have been successful with the following code:
(I am moving the most recent file to another folder, where I am then opening it).

Code:
Dim FSO As Object
    Dim FromPath As String
    Dim ToPath As String
    Dim Fdate As Date
    Dim FileInFromFolder As Object

    FromPath = "W:\SomeFolder"  
    ToPath = "W:\AnotherFolder"    

    If Right(FromPath, 1) <> "\" Then
        FromPath = FromPath & "\"
    End If

    If Right(ToPath, 1) <> "\" Then
        ToPath = ToPath & "\"
    End If

    Set FSO = CreateObject("scripting.filesystemobject")

    If FSO.FolderExists(FromPath) = False Then
        MsgBox FromPath & " doesn't exist"
        Exit Sub
    End If

    If FSO.FolderExists(ToPath) = False Then
        MsgBox ToPath & " doesn't exist"
        Exit Sub
    End If

    For Each FileInFromFolder In FSO.getfolder(FromPath).Files
        Fdate = Int(FileInFromFolder.DateLastModified)
        If Fdate >= Now()-2 Then
            FileInFromFolder.Copy ToPath
        End If
    Next FileInFromFolder

    MsgBox "You can find the files from " & FromPath & " in " & ToPath

End Sub [\code]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top