How to sort and trim a directory list?

M

.:mmac:.

I am listing a set of files using the following script:

<% set directory = Server.CreateObject("Scripting.FileSystemObject")
set allfiles = directory.GetFolder(Server.MapPath("/mmm/lesson/"))
For Each directoryfile in allFiles.files
Response.Write "<a href='/mmm/lesson/" & directoryfile.name & "'>" &
directoryfile.name & "</a>"
Next %>

This displays great (thanks Roland!)but the (minor) problem is that the
names are WeekX.pdf where X= week of year.
So the list is displayed as :
Week1.pdf
Week10.pdf
Week11.pdf
Week12.pdf
....
Week19.pdf
Week2.pdf
Week20.pdf
etc

How can I display the list numerical order and also trim it so that the
".pdf" is not shown?
I imagine there is trim function I can use to remove the extension, but I
have no clue how to sort these numerically this way.
 
K

Kyle Peterson

you cannot easily sort them numerically because of the way they are named
(I dont think.. but I am tired so maybe I am not thinking right)

it they were named more like this like this you could

Week001.pdf
Week010.pdf
Week011.pdf
Week012.pdf

then on to the next question

easiest way to remobe the ".pdf" is with a replace stament on each one

replace(whatever,".pdf","")

replacing the ".pdf" with nothing
 
K

Kyle Peterson

then you get into the "how you gonna sort them" question from technical
viewpoint

either your going to put it in an array and run comlxs sorting functions on
it
or put it in database table and let a SQL statement do the work
 
D

Dave Anderson

..:mmac:. said:
So the list is displayed as :
Week1.pdf
Week10.pdf
Week11.pdf
Week12.pdf
...
Week19.pdf
Week2.pdf
Week20.pdf
etc

How can I display the list numerical order and also trim it so that
the ".pdf" is not shown?

If the files are in a JScript array, this will suffice:

myArray.sort(function(a,b){return+a.replace(/\D/g,"")<+b.replace(/\D/g,"")?-1:1})

The VBScript analog seems tedious. You could do a 2D array (text and numeric
equivalent) and sort, or perhaps a simple array with numeric values only --
sort the numbers, then reassemble filenames.

Unfortunately for you, enumerator objects (or direct access to collections
in VBScript) do not have built-in ordering.


--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
 
D

Dave Anderson

..:mmac:. said:
Alas... thanks, I will live with the ordering as is.

You could always *utilize* my JScript suggestion. Start by encapsulating the
JScript logic in a function:

function listFiles(filePath) {
var fso = Server.CreateObject("Scripting.FileSystemObject"),
files = new Enumerator(fso.GetFolder(filePath).Files)
for (var a=[]; !files.atEnd(); files.moveNext())
a.push(files.item().name)
a.sort(function(x,y){return+x.replace(/\D/g,"")<+y.replace(/\D/g,"")?-1:1})
return a.join("*")
}


Now you need only call that function from elsewhere in your ASP script. Your
script can be written in either VBScript or JScript; this example is
obviously in VBScript:

<%@ Language=VBScript %><%
Dim myFiles
myFiles = Split(listFiles("c:\inetpub\wwwroot\"),"*")

' The rest of your logic goes here

%>
<script runat="server" language="JScript">
//----- function goes here -----//
</script>


Because VBScript arrays and JScript arrays don't play together especially
well, I used Array.join() to zip the JScript array into an
asterisk-delimited[1] string, and Split() to turn it back into a VBScript
array.

This example, incidentally, illustrates a very useful concept in ASP: mixing
languages. You can access JScript objects from VBScript blocks and
vice-versa. But due to order-of-execution rules, using objects other than
functions can yield unexpected results[2].



[1] Use whatever you like. To avoid splitting on a character that is
potentailly part of a filename, make sure you use an illegal filename
character for your delimiter. With that in mind, note that Windows does not
allow the following characters in filenames: \/:*?"<>| You could
likewise us whitespace characters, such as "\t" and vbTab, or "\r\n" and
vbCrLf.
[2] Though predictible ones. See http://aspfaq.com/show.asp?id=2045 for more
on this topic.

--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
 
M

.:mmac:.

Thanks that is intriguing, I'll try it this weekend, Hovever my first look
at this section :

myFiles = Split(listFiles("c:\inetpub\wwwroot\"),"*")

It looks like that would be in the visable source of the page, I would
prefer to not reveal the files location to the browser, can that be run a
the server end or otherwise hidden from the end user?

Dave Anderson said:
.:mmac:. said:
Alas... thanks, I will live with the ordering as is.

You could always *utilize* my JScript suggestion. Start by encapsulating
the
JScript logic in a function:

function listFiles(filePath) {
var fso = Server.CreateObject("Scripting.FileSystemObject"),
files = new Enumerator(fso.GetFolder(filePath).Files)
for (var a=[]; !files.atEnd(); files.moveNext())
a.push(files.item().name)

a.sort(function(x,y){return+x.replace(/\D/g,"")<+y.replace(/\D/g,"")?-1:1})
return a.join("*")
}


Now you need only call that function from elsewhere in your ASP script.
Your
script can be written in either VBScript or JScript; this example is
obviously in VBScript:

<%@ Language=VBScript %><%
Dim myFiles
myFiles = Split(listFiles("c:\inetpub\wwwroot\"),"*")

' The rest of your logic goes here

%>
<script runat="server" language="JScript">
//----- function goes here -----//
</script>


Because VBScript arrays and JScript arrays don't play together especially
well, I used Array.join() to zip the JScript array into an
asterisk-delimited[1] string, and Split() to turn it back into a VBScript
array.

This example, incidentally, illustrates a very useful concept in ASP:
mixing languages. You can access JScript objects from VBScript blocks and
vice-versa. But due to order-of-execution rules, using objects other than
functions can yield unexpected results[2].



[1] Use whatever you like. To avoid splitting on a character that is
potentailly part of a filename, make sure you use an illegal filename
character for your delimiter. With that in mind, note that Windows does
not
allow the following characters in filenames: \/:*?"<>| You could
likewise us whitespace characters, such as "\t" and vbTab, or "\r\n" and
vbCrLf.
[2] Though predictible ones. See http://aspfaq.com/show.asp?id=2045 for
more on this topic.

--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message.
Use
of this email address implies consent to these terms. Please do not
contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
 
M

.:mmac:.

Oh yea... I knew that...
thanks for the help.

Dave Anderson said:
Look again. That is sitting inside a <% %> block. If this had anything at
all to do with client-side processing, it would belong in a different
forum.


--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message.
Use of this email address implies consent to these terms. Please do not
contact me directly or ask me to contact you directly for assistance. If
your question is worth asking, it's worth posting.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top