473,395 Members | 1,442 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to sort and trim a directory list?

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.
Jul 22 '05 #1
13 2321
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
".:mmac:." <lost@sea> wrote in message
news:uI**************@TK2MSFTNGP09.phx.gbl...
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.

Jul 22 '05 #2
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

".:mmac:." <lost@sea> wrote in message
news:uI**************@TK2MSFTNGP09.phx.gbl...
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.

Jul 22 '05 #3
hmmm, that sounds like more work than it's worth.
"Kyle Peterson" <kp*****@hotmail.com> wrote in message
news:eo**************@TK2MSFTNGP09.phx.gbl...
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

".:mmac:." <lost@sea> wrote in message
news:uI**************@TK2MSFTNGP09.phx.gbl...
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.


Jul 22 '05 #4
well, pimpin aint easy
".:mmac:." <lost@sea> wrote in message
news:O$**************@TK2MSFTNGP12.phx.gbl...
hmmm, that sounds like more work than it's worth.
"Kyle Peterson" <kp*****@hotmail.com> wrote in message
news:eo**************@TK2MSFTNGP09.phx.gbl...
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

".:mmac:." <lost@sea> wrote in message
news:uI**************@TK2MSFTNGP09.phx.gbl...
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.



Jul 22 '05 #5
huh?

"Kyle Peterson" <kp*****@hotmail.com> wrote in message
news:uD**************@TK2MSFTNGP14.phx.gbl...
well, pimpin aint easy
".:mmac:." <lost@sea> wrote in message
news:O$**************@TK2MSFTNGP12.phx.gbl...
hmmm, that sounds like more work than it's worth.
"Kyle Peterson" <kp*****@hotmail.com> wrote in message
news:eo**************@TK2MSFTNGP09.phx.gbl...
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

".:mmac:." <lost@sea> wrote in message
news:uI**************@TK2MSFTNGP09.phx.gbl...
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.



Jul 22 '05 #6
it aint easy playin da game !

".:mmac:." <lost@sea> wrote in message
news:e7**************@tk2msftngp13.phx.gbl...
huh?

"Kyle Peterson" <kp*****@hotmail.com> wrote in message
news:uD**************@TK2MSFTNGP14.phx.gbl...
well, pimpin aint easy
".:mmac:." <lost@sea> wrote in message
news:O$**************@TK2MSFTNGP12.phx.gbl...
hmmm, that sounds like more work than it's worth.
"Kyle Peterson" <kp*****@hotmail.com> wrote in message
news:eo**************@TK2MSFTNGP09.phx.gbl...
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

".:mmac:." <lost@sea> wrote in message
news:uI**************@TK2MSFTNGP09.phx.gbl...
>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.
>



Jul 22 '05 #7
tru' dat!

"Kyle Peterson" <kp*****@hotmail.com> wrote in message
news:O3*************@TK2MSFTNGP15.phx.gbl...
it aint easy playin da game !

".:mmac:." <lost@sea> wrote in message
news:e7**************@tk2msftngp13.phx.gbl...
huh?

"Kyle Peterson" <kp*****@hotmail.com> wrote in message
news:uD**************@TK2MSFTNGP14.phx.gbl...
well, pimpin aint easy
".:mmac:." <lost@sea> wrote in message
news:O$**************@TK2MSFTNGP12.phx.gbl...
hmmm, that sounds like more work than it's worth.
"Kyle Peterson" <kp*****@hotmail.com> wrote in message
news:eo**************@TK2MSFTNGP09.phx.gbl...
> 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
>
>
>
> ".:mmac:." <lost@sea> wrote in message
> news:uI**************@TK2MSFTNGP09.phx.gbl...
>>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.
>>
>
>



Jul 22 '05 #8
..:mmac:. wrote:
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.
Jul 22 '05 #9
Alas... thanks, I will live with the ordering as is.

"Dave Anderson" <GT**********@spammotel.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
.:mmac:. wrote:
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.

Jul 22 '05 #10
..:mmac:. wrote:
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.
Jul 22 '05 #11
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" <GT**********@spammotel.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
.:mmac:. wrote:
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.

Jul 22 '05 #12
..:mmac:. wrote:

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

It looks like that would be in the visable source of the page...


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.
Jul 22 '05 #13
Oh yea... I knew that...
thanks for the help.

"Dave Anderson" <GT**********@spammotel.com> wrote in message
news:11*************@corp.supernews.com...
.:mmac:. wrote:

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

It looks like that would be in the visable source of the page...


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.

Jul 22 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mike Zupan | last post by:
I have a list that includes files and directories ie: list = I want to sort it so it looks like this I'm just wondering if there is an easy way to do this
4
by: Jake | last post by:
Im using the following code to display the contents of a directory: <% Set objFso = CreateObject("Scripting.FileSystemObject") Set objFiles = objFso.GetFolder(Server.MapPath(".")) Set fileList...
2
by: Peter S. Guild | last post by:
Hello, I have a simple routine that uses a dataview to hold information. I need to sort these columns by section and then by desc. No matter what I do the dataview will not sort. What am I...
1
by: gj | last post by:
Hi, I'm trying to sort a dropdownlist. I tried using .Sort but that doesn't work. can anyone help me? Below is the code for creating the list. foreach (DataRow quizRow in dsMain1.Tables.Rows)...
10
by: Amit | last post by:
Hi I'm having a problem using arraylist.sort. I'm using FileInfo.GetFiles() to receive a list of files from a directory. The files have a sequential number to start then theres either an "A"...
7
by: vzsnake | last post by:
Hello,everybody. One my class inhereted from System.Collections.CollectionBase in InnerList I add class B objects to InnerList and want to sort them by multiple properties. I done the sorting,...
4
by: rn5a | last post by:
Can the items in a ListBox be sorted by the name of the items? The ListBox actually lists all directories & files existing in a directory on the server. Note that all the directories should be...
3
by: aRTx | last post by:
I have try a couple of time but does not work for me My files everytime are sortet by NAME. I want to Sort my files by Date-desc. Can anyone help me to do it? The Script <? /* ORIGJINALI
12
by: Nzsquall | last post by:
Hi, I am having trouble sorting a client contact lists by their surname. Every time when I press the "sort by surname button, it always appear alphabetical order by clients' first name, because I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.