Results 1 to 12 of 12
  1. #1
    Join Date
    Sep 2003
    Location
    MI
    Posts
    3,713

    Unanswered: Using a FSO to get an ordered list of files in a directory ...

    Hi all.

    Once again, one of my patented difficult questions ...

    Using a FSO (FileSystemObject), I need to return the list of files in the folder in a sorted order (name, extension, type, or timestamp/date created/last modified) ... Anyone have an idea on how it can be done? There seems to be no way within the FSO itself or the GetFolder object either ...

    Thanks!

    - Mike
    Back to Access ... ADO is not the way to go for speed ...

  2. #2
    Join Date
    Jul 2003
    Location
    Michigan
    Posts
    1,941
    I use FSO quite a bit, and short of loading all the file names, dates, etc
    into a temporary table for sorting....
    I got nothing
    Inspiration Through Fermentation

  3. #3
    Join Date
    Sep 2003
    Location
    MI
    Posts
    3,713
    Gee, Thanks Red ...

    The only advice I seem to see on this is to pull the files into an array and sort the things yourself ...
    Back to Access ... ADO is not the way to go for speed ...

  4. #4
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Do you know any other collections you can specify the order of retrieval in?

    There is an alternative of course - you could loop recursively through the folder populating the array in sorted order to save sorting your array afterwards.
    Testimonial:
    pootle flump
    ur codings are working excelent.

  5. #5
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Do you know any other collections you can specify the order of retrieval in?

    There is an alternative of course - you could loop recursively through the folder populating the array in sorted order to save sorting your array afterwards. HTH
    Testimonial:
    pootle flump
    ur codings are working excelent.

  6. #6
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Coo.... dunno why that happened.
    Testimonial:
    pootle flump
    ur codings are working excelent.

  7. #7
    Join Date
    Sep 2003
    Location
    MI
    Posts
    3,713
    Quote Originally Posted by pootle flump
    Do you know any other collections you can specify the order of retrieval in?

    There is an alternative of course - you could loop recursively through the folder populating the array in sorted order to save sorting your array afterwards. HTH
    Do tell ... Using a FSO, you MAY get the files ordered the way desired (in my case, Date DESC) but there is no guarantee ... This ordering is ASSUMING that the directory has been ordered (say using the explorer and picking the dir and clicking on the column to order by) and it sticks with using the FSO. Right now, I can't tell ...
    Back to Access ... ADO is not the way to go for speed ...

  8. #8
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Quote Originally Posted by M Owen
    This ordering is ASSUMING that the directory has been ordered (say using the explorer and picking the dir and clicking on the column to order by) and it sticks with using the FSO. Right now, I can't tell ...
    Coo - I did not know that. So explorer actually reorders the index of the contents not merely the display? Perhaps then the answer lies in reordering the index....perhaps via means other than fso.
    Testimonial:
    pootle flump
    ur codings are working excelent.

  9. #9
    Join Date
    Sep 2003
    Location
    MI
    Posts
    3,713
    Quote Originally Posted by pootle flump
    Coo - I did not know that. So explorer actually reorders the index of the contents not merely the display? Perhaps then the answer lies in reordering the index....perhaps via means other than fso.
    I really do not know if it does ... It may, it may not. Gut: NOT. Like you pointed out, explorer is just a VIEW of the dir and files ...
    Back to Access ... ADO is not the way to go for speed ...

  10. #10
    Join Date
    Mar 2005
    Posts
    22
    In theory why couldn't you append the FSO values into a recordset. I have included some code but it is for a webpage that lets the use select the order they would like to see. But this should give you some ideas on how to order the files.

    <%
    '**********
    'kc_fsoFiles
    'Purpose:
    ' 1. To create a recordset using the FSO object and ADODB
    ' 2. Allows you to exclude files from the recordset if needed
    'Use:
    ' 1. Call the function when you're ready to open the recordset
    ' and output it onto the page.
    ' example:
    ' Dim rsFSO, strPath
    ' strPath = Server.MapPath("\PlayGround\FSO\Stuff\")
    ' Set rsFSO = kc_fsoFiles(strPath, "_")
    ' The "_" will exclude all files beginning with
    ' an underscore
    '**********
    Function kc_fsoFiles(theFolder, Exclude)
    Dim rsFSO, objFSO, objFolder, File
    Const adInteger = 3
    Const adDate = 7
    Const adVarChar = 200

    'create an ADODB.Recordset and call it rsFSO
    Set rsFSO = Server.CreateObject("ADODB.Recordset")

    'Open the FSO object
    Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

    'go get the folder to output it's contents
    Set objFolder = objFSO.GetFolder(theFolder)

    'Now get rid of the objFSO since we're done with it.
    Set objFSO = Nothing

    'create the various rows of the recordset
    With rsFSO.Fields
    .Append "Name", adVarChar, 200
    .Append "Type", adVarChar, 200
    .Append "DateCreated", adDate
    .Append "DateLastAccessed", adDate
    .Append "DateLastModified", adDate
    .Append "Size", adInteger
    .Append "TotalFileCount", adInteger
    End With
    rsFSO.Open()

    'Now let's find all the files in the folder
    For Each File In objFolder.Files

    'hide any file that begins with the character to exclude
    If (Left(File.Name, 1)) <> Exclude Then
    rsFSO.AddNew
    rsFSO("Name") = File.Name
    rsFSO("Type") = File.Type
    rsFSO("DateCreated") = File.DateCreated
    rsFSO("DateLastAccessed") = File.DateLastAccessed
    rsFSO("DateLastModified") = File.DateLastModified
    rsFSO("Size") = File.Size
    rsFSO.Update
    End If

    Next

    'And finally, let's declare how we want the files
    'sorted on the page. In this example, we are sorting
    'by File Type in descending order,
    'then by Name in an ascending order.
    rsFSO.Sort = "Type DESC, Size ASC "

    'Now get out of the objFolder since we're done with it.
    Set objFolder = Nothing

    'now make sure we are at the beginning of the recordset
    'not necessarily needed, but let's do it just to be sure.
    rsFSO.MoveFirst()
    Set kc_fsoFiles = rsFSO

    End Function

    'Now let's call the function and open the recordset on the page
    'the folder we will be displaying
    Dim strFolder : strFolder = Server.MapPath("\PlayGround\FSO\stuff\")

    'the actual recordset we will be creating with the kc_fsoFiles function
    Dim rsFSO 'now let's call the function and open the recordset

    'we will exclude all files beginning with a "_"
    Set rsFSO = kc_fsoFiles(strFolder, "_")

    'now we'll create a loop and start displaying the folder
    'contents with our recordset. Of course, this is just a
    'simple example and not very well formatted, i.e., not in
    'a table, but it gets the point across on how you can
    'ouput the recordset on the page.
    While Not rsFSO.EOF
    %>
    <p><%= rsFSO("Name").Value %> | <%= rsFSO("Type").Value %></p>
    <%
    'and let's move to the next record
    rsFso.MoveNext()
    Wend

    'finally, close out the recordset
    rsFSO.close()
    Set rsFSO = Nothing
    %>

  11. #11
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Hi asalamay

    I suspect Mike knows how to do that - he does afterall style himself as the Grand Poobar .

    I think this is more of a challenge to come up with something elegent rather than something that "does the job" - I think he is trying how to figure out how to write less verbose code anyway.

    Naturally one could (once again) be wrong
    Testimonial:
    pootle flump
    ur codings are working excelent.

  12. #12
    Join Date
    Sep 2003
    Location
    MI
    Posts
    3,713
    Quote Originally Posted by pootle flump
    Hi asalamay

    I suspect Mike knows how to do that - he does afterall style himself as the Grand Poobar .

    I think this is more of a challenge to come up with something elegent rather than something that "does the job" - I think he is trying how to figure out how to write less verbose code anyway.

    Naturally one could (once again) be wrong
    You know me Poots ... Grand Poobar? Actually I did find what I was looking for ... The FSO does have the other file attributes with it ... I just didn't know what the damn things were named ...

    MyFSO.Name
    MyFSO.DateCreated
    MyFSO.DateLastModified
    ...

    Unfortunately, there is no ordering mechanism with it ... I had to use an array and sort it to get the proper order for displaying the last 1000 files ...
    Back to Access ... ADO is not the way to go for speed ...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •