Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sorting list of file names with "month" in the name

Status
Not open for further replies.

jamescpp

IS-IT--Management
Aug 29, 2001
70
US
I'm not much of an ASP programmer. I'm working on a very small app to build a basic web page with url links to some pdfs. I have the main functionality done and working but the problem is sorting. I need to sort the list which I'm not sure how to do. To make it worse, I need to sort the files by the month and year for each document, which is part of the file name.

Can anyone show me how to sort this?

Here's what I have so far:
<% function MapURL(path)

dim rootPath, url

'Convert a physical file path to a URL for hypertext links.

rootPath = Server.MapPath("/")
url = Right(path, Len(path) - Len(rootPath))
MapURL = Replace(url, "\", "/")

end function %>


<%

Dim path1, item
path1 = "/letters/"

set fs = CreateObject("Scripting.FileSystemObject")

set folder = fs.GetFolder("E:\inetpub\
Set fc = folder.files

for each item in folder.SubFolders
ListFolderContents(item.Path)
next

for each item in folder.Files
url = MapURL(item.path)
Response.Write("<li><a href=""" & url & """>" _
& item.Name & "</a>" _
& "</li>" & vbCrLf)
next

%>

This give me a page of urls like this:
XYZ APRIL 2006 LETTER.pdf
XYZ AUGUST 2006 LETTER.pdf
XYZ DECEMBER 2006 LETTER.pdf
XYZ FEBRUARY 2006 LETTER.pdf
XYZ DECEMBER 2007 LETTER.pdf
XYZ AUGUST 2007 LETTER.pdf

The files are generally in this form. Any suggestions, preferrably examples on how to handle this so the output would be:
XYZ AUGUST 2007 LETTER.pdf
XYZ DECEMBER 2007 LETTER.pdf
XYZ FEBRUARY 2006 LETTER.pdf
XYZ APRIL 2006 LETTER.pdf
XYZ AUGUST 2006 LETTER.pdf
XYZ DECEMBER 2006 LETTER.pdf

Any help is appreciated!

JAmes
 
It would be better if you wrote them by year and numeric month.

xyz 2007 08
xyz 2007 12
xyz 2006 02
xyz 2006 04

If you look at the numeric sequence, the later ones are higher numbers than the earlier ones so it makes it really easy to sort.
 
Unfortunately I have no control over the file names. I just have to deal with them. Let's say they were in the format you suggest, How would I sort them?

Thanks,
James
 
You've got the names sorted in descending year and ascending month. Here is a sample of how to tag something to the data, sort it and print out the result. This is a .wsf file which you can just run on a command prompt. You'll have to modify it for asp. The <object> bit is the same as CreateObject.

In fact, I'd actually advise you to run your code as a wsf script first, debug it and then convert to asp. It is a lot easier than debugging asp.
Code:
<job>
<object id="objFSO" progid="Scripting.FileSystemObject"/>
<object id="dicMonth" progid="Scripting.Dictionary"/>
<script language="vbscript">

sub MonthPopulate
   dicMonth.item("January") = "12"
   dicMonth.item("February") = "11"
   dicMonth.item("March") = "10"
   dicMonth.item("April") = "09"
   dicMonth.item("May") = "08"
   dicMonth.item("June") = "07"
   dicMonth.item("July") = "06"
   dicMonth.item("August") = "05"
   dicMonth.item("September") = "04"
   dicMonth.item("October") = "03"
   dicMonth.item("November") = "02"
   dicMonth.item("December") = "01"
end sub

dim testdata, sortdata(), parts, keyeddata, keym, i, j, lower 
const IXMON = 1, IXYEAR = 2
testdata = array ( _
   "abc July 2007.pdf", _
   "abc April 2006.pdf", _
   "abc January 2007.pdf", _
   "abc June 2006.pdf", _
   "abc December 2006.pdf", _
   "abc May 2006.pdf", _
   "abc November 2007.pdf")
   
MonthPopulate

' Create array for sorting   
redim sortdata(ubound(testdata))

' tag on sort criteria
for i = 0 to ubound(testdata)
   parts = split (testdata(i), " ")
   keyeddata = left(parts(IXYEAR), 4)
   keym = parts(IXMON)
   if dicMonth.exists(keym) then
      keym = dicMonth.item(keym)
   else
      keym = "00"
   end if
   keyeddata = keyeddata & keym
   keyeddata = keyeddata & testdata(i)
   sortdata(i) = keyeddata
next

' simple selection sort
for i = 0 to ubound(sortdata) - 1
   lower = i
   for j = i + 1 to ubound(sortdata)
      if sortdata(lower) < sortdata(j) then lower = j
   next
   if lower <> i then
      temp = sortdata(i)
      sortdata(i) = sortdata(lower)
      sortdata(lower) = temp
   end if
next      

' Dump the results without the tag
for i = 0 to ubound(sortdata)
   WScript.Echo mid (sortdata(i), 7)
next
</script>
</job>
 
The sort method I'm using is called selection sort. It is OK for up to 50 items. Any more than that, I'd recommend ShellSort or quicksort. Just google for it - the algorithms are quite simple.

With shellsort, there may be some discussion about the spacing. Different sites may use different spacing but this is very data dependent.

You will generally find two different algorithms for quicksort - a recursive and a non recursive one.

Don't mix up quicksort with quickersort. They are slightly different algorithms.

I don't know if WQL has a sort keyword like SQL. If it does, you could use it in conjunction with WMI.
 
Thanks. I'm trying to do this using your code as a guideline. I'm trying to trap for files that do not match the expected standard, otherwise the thing bombs. So I'm trying to match for file starting with "abc ". I keep getting end of line expected, or objects expected, etc. I've tried MANY iterations, I have an ASP book (not really helpful) and searching with google...

I can list the filenames with item.Name. How can I check that the filename starts with "abc "? I've tried all kinds of things, but so far I cannot get it to work. I'm trying to store all the filenames that start with abc in one array, and store all the others in another array.

dim data(100), otherdata(100)
dim tempname
arraycounter = 0
arraycounter2 = 0
for each item in folder.Files
tempname = item.Name
' if (tempname.StartsWith("abc ")) then
' data(arraycounter) = item.Name
' arraycounter = arraycounter + 1
' else
' otherdata(arraycounter2) = item.Name
' arraycounter2 = arraycounter2 + 1
' end if
next

I apologize for having to ask dumb questions but now I'm totally frustrated. Again, I'm not an asp programmer, although I've done a lot in some other languages, like java. I was expecting that with a little googling I'd figure it out. Other things I've been successful, but not this one!

All the help is appreciated.

Thanks,
James
 
Try

if left(tempname,4) = "abc " then ...
 
Okay, that worked! Now on to the next one. I keep getting this error when it tries to do the tagging:
Subscript out of range: '[number: 2]'

This is happening on the line:
keyeddata = left(parts(IXYEAR), 4)

My understanding of the error is that it usually means you tried to access an element of an array that was either greater than its ubound (in this case). But I don't see why that's happening.

<snip>
arraycounter = 0
arraycounter2 = 0

for each item in folder.Files
tempname = item.Name
if left(tempname,4) = "abc " then
data(arraycounter) = item.Name
arraycounter = arraycounter + 1
else
otherdata(arraycounter2) = item.Name
arraycounter2 = arraycounter2 + 1
end if
next

' Create array for sorting
redim sortdata(ubound(data))

' tag on sort criteria
for i = 0 to ubound(data)
parts = split(data(i), " ")
keyeddata = left(parts(IXYEAR), 4)
keym = parts(IXMON)
if dicMonth.exists(keym) then
keym = dicMonth.item(keym)
else
keym = "00"
end if
keyeddata = keyeddata & keym
keyeddata = keyeddata & data(i)
sortdata(i) = keyeddata
next

The good news is I think it's almost there though!

Thanks,
James
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top