Click to See Complete Forum and Search --> : List files in this order


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
%>

jrthor2
07-25-2003, 07:54 AM
Yes, I know that. The files in that directory are named like (not necessarily in that order):

January 2003.pdf
February 2003.pdf
December 2002.pdf

Is it possible to show these with the current month's at the top and then decending according the calendar months/year??

Like this:

July 2003
June 2003
May 2003
March 2003
February 2003
January 2003
December 2002
November 2002

khaki
07-25-2003, 08:56 AM
hi jr....

maybe create a Select Case statement to convert the month name to it's cooresponding number prior to creating the recordset (and then back again for listing it)?

It's a bit of work (breaking apart the string and then reconstructing it again)....
but it's a lesson learned about planning ahead when deciding on a naming convention for files (yyyymmdd.pdf always works best).

You probably already considered something like this...
but I don't see another way to handle it (Month names are for people :rolleyes: Month numbers are for 'puters :) ).

finally a non-humid day in the Garden State :) yay!!! ....
;) k

jrthor2
07-25-2003, 09:00 AM
Yes, I considered that, but this is a for my site management application, and for people that are not computer literate, I made the assumption that naming their files with month names would be easier for them than mmddyyyy.pdf. Anyway, could you show me how to do what you suggested??

Or, if Ihad them name their files mmddyyyy, how could I do it this way??

khaki
07-25-2003, 09:43 AM
fair enough....
but anyone who can't understand 20030626.pdf is a bit of a moron, no?
but whatever.

and it should NOT be mmddyyyy.pdf as you stated (that will not sort the files into groups of years).
It should be yyyymmdd.pdf

anyway....
you will need to break the string (filename) using Len, Left, and Mid functions.

I'm not gonna do it for you but here is a longhand sample to get you started:

MonthLength = Len(filename)-9
note: this assumes that there is a space(?) or an underscore seperating the month name from the year. If not it's "-8" instead.

JustMonth = Left(filename, MonthLength)

JustYear = Mid(filename,MonthLength +2,4)
note: the same assumption applies here as well. Otherwise it's "MonthLegnth+1"

Once you have the month name extracted...
create a basic Select Case statement:

<% Select Case JustMonth
Case "january"
MoNum = 1
Case "february"
MoNum = 2
Case "march"
MoNum = 3
...etc
End Select
%>

Then loop the Case statement into the recordset code that you have (but be sure to create individual fields for the month and the year).

Then just reverse the process and concatenate the string back together for the dimwitted few who don't understand dates that are expressed as numbers :)
(hey.... you wanted to make your file naming convention easy for stupid people? This is what you have to go through to let them remain stupid :rolleyes: )

I could have done the code for you myself....
but... feed a man a fish and he eats for a day....
teach a man to fish and he eats for a lifetime :)
(maybe you'll get lucky and someone will do it all for you - and in shorthand!!! :) )

Hope you can grasp the concept though.
I'll be back on in a few hours.
If you haven't figured it out by then (or if no one else has helped you)...
I'll see what else I can do for you then.

Have fun (and just remember.... the stupid people are laughing at YOU now!!! :) just kidding :) )

;) k

jrthor2
07-25-2003, 10:21 AM
Ok, trying to muddle my way through this. I had to modify your code a little, because it wasn't giving me the year correctly and it is not giving me the month correctly. Here is what I have (also, the files are now names 20030701.pdf, etc):


For Each objItem In objFolder.Files
rstFiles.AddNew
rstFiles.Fields("name").Value = objItem.Name
MonthLength = Len(rstFiles.Fields("name").Value)-9
'response.write(MonthLength) & "<br>"
fileMonth = Left(rstFiles.Fields("name").Value,MonthLength)
fileYear = Mid(rstFiles.Fields("name").Value,MonthLength -4,4)
Select Case fileMonth
Case "January"
MoNum = 1
Case "February"
MoNum = 2
Case "March"
MoNum = 3
Case "April"
MoNum = 4
Case "May"
MoNum = 5
Case "June"
MoNum = 6
Case "July"
MoNum = 7
Case "August"
MoNum = 8
Case "September"
MoNum = 9
Case "October"
MoNum = 10
Case "November"
MoNum = 11
Case "December"
MoNum = 12
End Select
rstFiles.Fields("size").Value = objItem.Size
rstFiles.Fields("date").Value = objItem.DateCreated
rstFiles.Fields("type").Value = objItem.Type
rstFiles.Fields("month").Value = fileMonth
rstFiles.Fields("year").Value = fileYear

Response.write(rstFiles.Fields("year").Value)

Next 'objItem

jrthor2
07-25-2003, 10:33 AM
Actually, I think I got it using the my original naming convention (January 2003.pdf), but there is 1 other issue after my code below:

For Each objItem In objFolder.Files
rstFiles.AddNew
rstFiles.Fields("name").Value = objItem.Name
MonthLength = Len(rstFiles.Fields("name").Value)-9
'response.write(MonthLength) & "<br>"
fileMonth = Left(rstFiles.Fields("name").Value,MonthLength)
fileYear = Mid(rstFiles.Fields("name").Value,MonthLength +2,4)
Select Case fileMonth
Case "January"
MoNum = 1
Case "February"
MoNum = 2
Case "March"
MoNum = 3
Case "April"
MoNum = 4
Case "May"
MoNum = 5
Case "June"
MoNum = 6
Case "July"
MoNum = 7
Case "August"
MoNum = 8
Case "September"
MoNum = 9
Case "October"
MoNum = 10
Case "November"
MoNum = 11
Case "December"
MoNum = 12
End Select
rstFiles.Fields("size").Value = objItem.Size
rstFiles.Fields("date").Value = objItem.DateCreated
rstFiles.Fields("type").Value = objItem.Type
rstFiles.Fields("month").Value = MoNum
rstFiles.Fields("year").Value = fileYear

'Response.write(rstFiles.Fields("month").Value)

Next 'objItem

Now comes a tricky part, if there is afile in that directory that is called something like "Special Congregational Meeting - June 2003.pdf", is it possible to show that file under the regular 'June 2003" file?? Right now, it shows that file between January 2003 and February 2003.

jrthor2
07-26-2003, 07:06 AM
Could I get an example, this is getting pretty complicated for a fairly newbie?