FSO - Get name of most recent file

I have a folder full of text files. I need to find the name of the most recently dated file. My code below doesn't find any files if one doesn't exist for the day before yesterday. I know why but how can I fix it? Thanks!
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(Server.MapPath("/someFolder/"))
Set colFiles = objFolder.Files
dtmNewestDate = Now -1
For Each objFile in colFiles
    If objFile.DateCreated > dtmNewestDate Then
        dtmNewestDate = objFile.DateCreated
        strNewestFile = objFile.Name
    End If
mostRecentFile = strNewestFile
Set objFolder = Nothing
Set colFiles = Nothing

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

remove this condition: If objFile.DateCreated > dtmNewestDate Then

Then use a dictionary object or something to store each LastModDate (not DateCreated), and at the end of your loop, find the one with the most recent date.
b0lsc0ttIT ManagerCommented:

Similar idea to what SweatCoder recommended but I have used a Recordset object instead of the Dictionary object.  Make a new Recordset and create fields for filename, date, etc.  Fill the recordset with the files and then I sort by the date field.   I can provide some sample code if you need it and are interested.  If the Dictionary object works then great but I usually found I need the fields and sort ability of a Recordset object.

Let me know if you have any questions or need more information.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
yourbudweiserAuthor Commented:
The method I chose was to insert the file information involved into an ADO recordset and then use the built in recordset sorting capabilities as found on ASP 101.

Thanks for responding.
  Const adVarChar = 200
  Const adInteger = 3
  Const adDate = 7
  strPath = Server.MapPath("/path obfuscated/")
  Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
  Set objFolder = objFSO.GetFolder(Server.MapPath("/path obfuscated/"))
  Set rstFiles = Server.CreateObject("ADODB.Recordset")
  rstFiles.Fields.Append "name", adVarChar, 255
  rstFiles.Fields.Append "date", adDate
  For Each objItem In objFolder.Files
  	rstFiles.Fields("name").Value = objItem.Name
  	rstFiles.Fields("date").Value = objItem.DateCreated
  Next 'objItem
  Set objItem = Nothing
  Set objFolder = Nothing
  Set objFSO = Nothing
  rstFiles.Sort = "date DESC"
  response.write rstFiles.Fields("name").Value
  ' Close our ADO Recordset object
  Set rstFiles = Nothing

Open in new window

b0lsc0ttIT ManagerCommented:
Sounds like the exact method I suggested.  I am glad it helped.  Let us know if you have a question closing this or need more help getting it to work.  It looks like you know how to do it so I doubt you need anymore help but let us know.

Force accepted.
EE Admin
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.