jrthor2
07-24-2003, 10:36 AM
I have 2 pages that list monthly Newsletters and I have the page dynamically generating the list of files that are in the pdf directory. All the files are named with the Month, then a space, then the year. Is it possible to list these files in the order of a calendar year, starting with January and ending with December? Right the list starts with April, and ends with November. Here is the code I am using to list the files:
<%' Now to the Runtime code:
Dim strPath 'Path of directory to show
Dim objFSO 'FileSystemObject variable
Dim objFolder 'Folder variable
Dim objItem 'Variable used to loop through the contents of the folder
' A recordset object variable and some selected constants from adovbs.inc.
' I use these for the sorting code.
Dim rstFiles
Const adVarChar = 200
Const adInteger = 3
Const adDate = 7
' You could just as easily read this from some sort of input, but I don't
' need you guys and gals roaming around our server so I've hard coded it to
' a directory I set up to illustrate the sample.
' NOTE: As currently implemented, this needs to end with the /
If Request.QueryString("path") <> "" Then
strPath = Request.QueryString("path")
Else
strPath = "/newsletter/pdf/"
End If
' Create our FSO
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
' Get a handle on our folder
Set objFolder = objFSO.GetFolder(Server.MapPath(strPath))
' First I deal with any subdirectories. I just display them and when you
' click you go to them via plain HTTP. You might want to loop them back
' through this file once you've set it up to take a path as input. It seems
' like the logical thing to do to me at least!
For Each objItem In objFolder.SubFolders
' Deal with the stupid VTI's that keep giving our visitors 404's
If InStr(1, objItem, "_vti", 1) = 0 Then
End If
Next 'objItem
' Now that I've done the SubFolders, do the files!
' In order to be able to sort them easily and still close the FSO relatively
' quickly I'm going to make use of an ADO Recordset object with no attached
' datasource. While it does have a slightly greater overhead then an array
' or dictionary object, it gives me named access to the fields and has built
' in sorting functionality.
Set rstFiles = Server.CreateObject("ADODB.Recordset")
rstFiles.Fields.Append "name", adVarChar, 255
rstFiles.Fields.Append "size", adInteger
rstFiles.Fields.Append "date", adDate
rstFiles.Fields.Append "type", adVarChar, 255
rstFiles.Open
For Each objItem In objFolder.Files
rstFiles.AddNew
rstFiles.Fields("name").Value = objItem.Name
rstFiles.Fields("size").Value = objItem.Size
rstFiles.Fields("date").Value = objItem.DateCreated
rstFiles.Fields("type").Value = objItem.Type
Next 'objItem
' All done! Kill off our File System Object variables.
Set objItem = Nothing
Set objFolder = Nothing
Set objFSO = Nothing
' Now we can sort our data and display it:
' Sort ascending by size and secondarily descending by date
' (by date is mainly for illustration since all our files
' are different sizes)
rstFiles.Sort = "name ASC"
If rstFiles.EOF Then
Response.Write "<b><font class=fontsize5>No files</font></b>"
Else
rstFiles.MoveFirst
Do While Not rstFiles.EOF
file_nme = rstFiles.Fields("name").Value
file_nme = Left(file_nme, InStrRev(file_nme, ".") - 1)
%>
<tr>
<td>
<strong>
<% If Request.QueryString("path") <> "" Then %>
<a href="/reports/track.asp?r=<%=strPath & _
("/" & rstFiles.Fields("name").Value)%>"><%= file_nme %></a>
<% Else %>
<a href="/reports/track.asp?r=<%=strPath & _
rstFiles.Fields("name").Value%>"><%= file_nme %></a>
</strong>
<% End If %>
</td>
</tr>
<%
rstFiles.MoveNext
Loop
' Close our ADO Recordset object
rstFiles.Close
Set rstFiles = Nothing
End If
'Close the table
%>
<%' Now to the Runtime code:
Dim strPath 'Path of directory to show
Dim objFSO 'FileSystemObject variable
Dim objFolder 'Folder variable
Dim objItem 'Variable used to loop through the contents of the folder
' A recordset object variable and some selected constants from adovbs.inc.
' I use these for the sorting code.
Dim rstFiles
Const adVarChar = 200
Const adInteger = 3
Const adDate = 7
' You could just as easily read this from some sort of input, but I don't
' need you guys and gals roaming around our server so I've hard coded it to
' a directory I set up to illustrate the sample.
' NOTE: As currently implemented, this needs to end with the /
If Request.QueryString("path") <> "" Then
strPath = Request.QueryString("path")
Else
strPath = "/newsletter/pdf/"
End If
' Create our FSO
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
' Get a handle on our folder
Set objFolder = objFSO.GetFolder(Server.MapPath(strPath))
' First I deal with any subdirectories. I just display them and when you
' click you go to them via plain HTTP. You might want to loop them back
' through this file once you've set it up to take a path as input. It seems
' like the logical thing to do to me at least!
For Each objItem In objFolder.SubFolders
' Deal with the stupid VTI's that keep giving our visitors 404's
If InStr(1, objItem, "_vti", 1) = 0 Then
End If
Next 'objItem
' Now that I've done the SubFolders, do the files!
' In order to be able to sort them easily and still close the FSO relatively
' quickly I'm going to make use of an ADO Recordset object with no attached
' datasource. While it does have a slightly greater overhead then an array
' or dictionary object, it gives me named access to the fields and has built
' in sorting functionality.
Set rstFiles = Server.CreateObject("ADODB.Recordset")
rstFiles.Fields.Append "name", adVarChar, 255
rstFiles.Fields.Append "size", adInteger
rstFiles.Fields.Append "date", adDate
rstFiles.Fields.Append "type", adVarChar, 255
rstFiles.Open
For Each objItem In objFolder.Files
rstFiles.AddNew
rstFiles.Fields("name").Value = objItem.Name
rstFiles.Fields("size").Value = objItem.Size
rstFiles.Fields("date").Value = objItem.DateCreated
rstFiles.Fields("type").Value = objItem.Type
Next 'objItem
' All done! Kill off our File System Object variables.
Set objItem = Nothing
Set objFolder = Nothing
Set objFSO = Nothing
' Now we can sort our data and display it:
' Sort ascending by size and secondarily descending by date
' (by date is mainly for illustration since all our files
' are different sizes)
rstFiles.Sort = "name ASC"
If rstFiles.EOF Then
Response.Write "<b><font class=fontsize5>No files</font></b>"
Else
rstFiles.MoveFirst
Do While Not rstFiles.EOF
file_nme = rstFiles.Fields("name").Value
file_nme = Left(file_nme, InStrRev(file_nme, ".") - 1)
%>
<tr>
<td>
<strong>
<% If Request.QueryString("path") <> "" Then %>
<a href="/reports/track.asp?r=<%=strPath & _
("/" & rstFiles.Fields("name").Value)%>"><%= file_nme %></a>
<% Else %>
<a href="/reports/track.asp?r=<%=strPath & _
rstFiles.Fields("name").Value%>"><%= file_nme %></a>
</strong>
<% End If %>
</td>
</tr>
<%
rstFiles.MoveNext
Loop
' Close our ADO Recordset object
rstFiles.Close
Set rstFiles = Nothing
End If
'Close the table
%>