How to sort and trim a directory list?

Discussion in 'ASP General' started by .:mmac:., May 15, 2005.

  1. .:mmac:.

    .:mmac:. Guest

    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.
    .:mmac:., May 15, 2005
    #1
    1. Advertising

  2. 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:...
    >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.
    >
    Kyle Peterson, May 15, 2005
    #2
    1. Advertising

  3. 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:...
    >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.
    >
    Kyle Peterson, May 15, 2005
    #3
  4. .:mmac:.

    .:mmac:. Guest

    hmmm, that sounds like more work than it's worth.


    "Kyle Peterson" <> wrote in message
    news:...
    > 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:...
    >>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.
    >>

    >
    >
    .:mmac:., May 15, 2005
    #4
  5. well, pimpin aint easy


    ".:mmac:." <lost@sea> wrote in message
    news:O$...
    > hmmm, that sounds like more work than it's worth.
    >
    >
    > "Kyle Peterson" <> wrote in message
    > news:...
    >> 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:...
    >>>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.
    >>>

    >>
    >>

    >
    >
    Kyle Peterson, May 15, 2005
    #5
  6. .:mmac:.

    .:mmac:. Guest

    huh?

    "Kyle Peterson" <> wrote in message
    news:...
    > well, pimpin aint easy
    >
    >
    > ".:mmac:." <lost@sea> wrote in message
    > news:O$...
    >> hmmm, that sounds like more work than it's worth.
    >>
    >>
    >> "Kyle Peterson" <> wrote in message
    >> news:...
    >>> 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:...
    >>>>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.
    >>>>
    >>>
    >>>

    >>
    >>

    >
    >
    .:mmac:., May 15, 2005
    #6
  7. it aint easy playin da game !

    ".:mmac:." <lost@sea> wrote in message
    news:...
    > huh?
    >
    > "Kyle Peterson" <> wrote in message
    > news:...
    >> well, pimpin aint easy
    >>
    >>
    >> ".:mmac:." <lost@sea> wrote in message
    >> news:O$...
    >>> hmmm, that sounds like more work than it's worth.
    >>>
    >>>
    >>> "Kyle Peterson" <> wrote in message
    >>> news:...
    >>>> 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:...
    >>>>>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.
    >>>>>
    >>>>
    >>>>
    >>>
    >>>

    >>
    >>

    >
    >
    Kyle Peterson, May 15, 2005
    #7
  8. .:mmac:.

    .:mmac:. Guest

    tru' dat!

    "Kyle Peterson" <> wrote in message
    news:...
    > it aint easy playin da game !
    >
    > ".:mmac:." <lost@sea> wrote in message
    > news:...
    >> huh?
    >>
    >> "Kyle Peterson" <> wrote in message
    >> news:...
    >>> well, pimpin aint easy
    >>>
    >>>
    >>> ".:mmac:." <lost@sea> wrote in message
    >>> news:O$...
    >>>> hmmm, that sounds like more work than it's worth.
    >>>>
    >>>>
    >>>> "Kyle Peterson" <> wrote in message
    >>>> news:...
    >>>>> 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:...
    >>>>>>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.
    >>>>>>
    >>>>>
    >>>>>
    >>>>
    >>>>
    >>>
    >>>

    >>
    >>

    >
    >
    .:mmac:., May 16, 2005
    #8
  9. ..: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.
    Dave Anderson, May 16, 2005
    #9
  10. .:mmac:.

    .:mmac:. Guest

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

    "Dave Anderson" <> wrote in message
    news:%...
    > .: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.
    >
    .:mmac:., May 16, 2005
    #10
  11. ..: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.
    Dave Anderson, May 17, 2005
    #11
  12. .:mmac:.

    .:mmac:. Guest

    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" <> wrote in message
    news:%...
    > .: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.
    >
    .:mmac:., May 18, 2005
    #12
  13. .:mmac:.

    .:mmac:. Guest

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

    "Dave Anderson" <> wrote in message
    news:...
    > .: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.
    >
    .:mmac:., May 20, 2005
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. FAQ server
    Replies:
    0
    Views:
    139
    FAQ server
    Aug 29, 2006
  2. FAQ server
    Replies:
    0
    Views:
    136
    FAQ server
    Oct 26, 2006
  3. FAQ server
    Replies:
    6
    Views:
    214
    Jonas Raoni
    Dec 25, 2006
  4. FAQ server
    Replies:
    26
    Views:
    295
    Dr J R Stockton
    Feb 26, 2007
  5. FAQ server
    Replies:
    2
    Views:
    131
    -Lost
    Apr 24, 2007
Loading...

Share This Page