Forum Moderators: open

Message Too Old, No Replies

finding last file created in folder (asp)

         

shakes911

11:13 am on Apr 3, 2001 (gmt 0)



hi, does anyone know how i can look at text files in a stated folder and retrieve the most recently created one?

cheers

Xoc

11:59 am on Apr 3, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use this:


<%
Function FindLastFile(strFolderUrl)
Dim fso
Dim fld
Dim fil
Dim filLastModified
Dim dateLastModified
Dim ts

Set fso = Server.CreateObject("Scripting.FileSystemObject")

'Find Last Modified file in a directory
'Get folder
Set fld = fso.GetFolder(Server.MapPath(strFolderUrl))

'walk through files in folder looking
'for last modified file, remember it
'when it is found
dateLastModified = #1/1/100#
For Each fil in fld.files
If fil.DateLastModified > dateLastModified Then
Set filLastModified = fil
dateLastModified = fil.DateLastModified
End If
Next

If Not (filLastModified Is Nothing) Then
'read the remembered file's contents
'to the return value of the function
Set ts = filLastModified.OpenAsTextStream()
FindLastFile = ts.ReadAll()
Call ts.Close()
Set ts = Nothing
Else
FindLastFile = ""
End If
Set fil = Nothing
Set fld = Nothing
Set fso = Nothing
End Function
%>

Then you could display the file with:


<pre>
<%=Server.HTMLEncode(FindLastFile("/_themes/xoc"))%>
</pre>

The basic scheme is to use the Scripting.FileSystemObject object to browse the directory structure. Then when you find the right file, save it. Then open it to a text stream and get its contents to a string and return it.