Files_MostRecentN

Gets most recent N files from a given folder.
Uses Arrays, and the function ArrSort_2Arr to sort files in descending order by date modified, then grab the top N files into ANStr.
You may decide separator, and number of files, folder should be passed as HTML UnMapPathed folder
If FileFilter = "", all file types will be returned
If FileFIlter starts with "." like ".txt", function will return files with that extension only
If FileFilter = "images", function will return image files only (jpg, png, gif and bmp)

Function Files_MostRecentN(InFolder_UnMapPathed, ReturnRecent4Files, FileFilter, Sepa)
 If InFolder_UnMapPathed = "" Then Exit Function
 If Cint(ReturnRecent4Files) = 0 Then ReturnRecent4Files = 4
 dim dic
 set dic     = CreateObject("Scripting.Dictionary")
 Set Di2     = CreateObject("Scripting.Dictionary")
 Set fso     = Server.CreateObject("Scripting.FileSystemObject")
 Set fo     = fso.GetFolder(Server.MapPath(InFolder_UnMapPathed))
 Recent1     = 0
 RecentN     = ""
 ' Response.Write "<h3>Files in folder</h3>"
 For Each x In fo.Files
  Cond1     = False
  FiiN     = X.Name
  If FileFilter = "" Then
   Cond1    = True
  Else
   If Left(FileFilter, 1) = "." Then
    Cond1 = UCase(Right(Fiin, Len(FileFilter))) = UCase(FileFilter)
   ElseIf UCase(FileFilter) = "IMAGES" Then
    Cond1 = UCase(Right(Fiin, 4)) = ".PNG" Or UCase(Right(Fiin, 4)) = ".JPG" Or _
     UCase(Right(Fiin, 4)) = ".GIF" Or UCase(Right(Fiin, 4)) = ".BMP"
   End IF
  End If
  If Cond1 Then Dic.Add Fiin, CDate(CLng(x.DateLastModified))
 Next
 A1       = Dic.Keys
 A2       = Dic.Items
 ' For I = 0 to UBound(A1)
  ' Response.Write I & " : " & A1(I) & " >> " & A2(I) & "
"
 ' Next
 ' Response.Write "
<hr/><h3>Sorted ascending by date modified</h3>"
 ' A3       = ArrSort_2Arr(A2, A1, 1)
 ' For I = 0 to UBound(A3)
  ' Response.Write I & " : " & A1(I) & " >- " & A3(I) & "
"
 ' Next
 ' Response.Write "
<hr/><h3>Sorted Descending by date modified</h3>"
 A4       = ArrSort_2Arr(A2, A1, 2)
 ' For I = 0 to UBound(A4)
  ' Response.Write I & " : " & A1(I) & " >- " & A4(I) & "
"
 ' Next
 ' Response.Write "
<hr/><h3>Function output</h3>"
 Rett      = ""
 On Error Resume Next
 For I = 0 to ReturnRecent4Files - 1
  If Rett > "" Then Rett = Rett & Sepa
  Rett     = Rett & A1(I)
  ' Response.Write I & " : " & A1(I) & " >- " & A4(I) & "
"
 Next
 ' Response.Write Rett
 Files_MostRecentN  = Rett
End Function

InFolder_UnMapPathed, ReturnRecent4Files, FileFilter, Sepa

Returns most recent 6 images from folder /folder2/<br/><br/>Response.Write Files_MostRecentN(&#34;/folder2/&#34;, 6, &#34;images&#34;, &#34;|&#34;)

Views 330 Downloads 100

'ArrSort_2Arr', 'FSO', 'ANStr', 'ANString', 'ANStrList', 'files', 'most recent', 'recent', 'date modified', 'filter', 'images', 'extension', 'match'

ANmarAmdeen
324
File System Classic ASP
Revisions

v1.0