[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 925
  • Last Modified:

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
Next
mostRecentFile = strNewestFile
Set objFolder = Nothing
Set colFiles = Nothing

Open in new window

0
yourbudweiser
Asked:
yourbudweiser
1 Solution
 
SweatCoderCommented:
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.
0
 
b0lsc0ttIT ManagerCommented:
yourbudweiser,

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.

b0lsc0tt
0
 
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
  rstFiles.Open
  
  For Each objItem In objFolder.Files
  	rstFiles.AddNew
  	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"
  rstFiles.MoveFirst
  
  response.write rstFiles.Fields("name").Value
  
  ' Close our ADO Recordset object
  rstFiles.Close
  Set rstFiles = Nothing

Open in new window

0
 
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.

bol
0
 
Computer101Commented:
Force accepted.
Computer101
EE Admin
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now