SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Oct 2003
    Location
    Tenerife, Spain / UK
    Posts
    329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Sorting Files using FileSystemObject

    Hi -

    Quick Question: Using the FileSystem Object in Classic ASP is there any way to read the files in a folder in the order they were created?

    Example:

    Code:
    Dim ObjFSO, ObjFile, ObjFolder, x
    Set ObjFSO = Server.CreateObject("Scripting.FileSystemObject")
    Set ObjFolder = ObjFSO.GetFolder(Server.MapPath("../myfolder"))
    x= 0
    
    For Each ObjFile in ObjFolder.Files
        Do while x <10
            Response.write(ObjFile.Name & " - " & ObjFile.DateCreated)
            x = x+1
        Loop
    Next
    This script reads in the first 10 files found in the designated folder, but how can I be sure of getting the 10 most recent files? It seems that the default ordering is by file name.

    The only method I have at the moment is to read in ALL the files attributes into Arrays and performing a Bubble Sort. This gets increasing inefficient as more files are added to the folder I am reading from.

    Is there any methods in the FileSystem or Folder Object which will allow me to sort the files by DateCreated or Modified before selecting the first 10 files I find?

    Thanks
    David Parkes
    Nuclear Internet - Windows Web Hosting
    http://www.nuclearinternet.com

  2. #2
    SitePoint Addict
    Join Date
    Oct 2003
    Location
    Tenerife, Spain / UK
    Posts
    329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually my looping logic is screwed up in the last example, but I think you get the idea.
    David Parkes
    Nuclear Internet - Windows Web Hosting
    http://www.nuclearinternet.com

  3. #3
    Original Gangster silver trophy Thing's Avatar
    Join Date
    Oct 2000
    Location
    Philadelphia, PA
    Posts
    4,708
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    There is no way to sort by date created with FSO. However I had this same issue a few years back. One work around is to use a disconnected recordset to do your sorting. I'll dig up my code and post it here for you to look at.

  4. #4
    SitePoint Addict
    Join Date
    Oct 2003
    Location
    Tenerife, Spain / UK
    Posts
    329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Thing :-)

    I'm actually writing my own Blog Software using XML as a datastore and ASP to transform the XML into XHTML on the fly.

    At the moment, I'm reading the FileName and DateCreated Attributes into Arrays, then performing a sort on the ArrayDateCreated values. This works OK but its going to get slower as more articles are added to the blog.

    I was hoping their would be an obscure FileSystem function to make this nice and simple. Oh well!

    Do you think your disconnected recordset solution will be more efficient?

    Here's the code for my Array Sorting if you are interested.

    Code:
    Dim max, i, j,TempVar1, TempVar2
    max=ubound(ArrFileCreated)
    'ArrFileName contains the name of each file in the collection.
    'ArrFileCreated contains the dates each file was created.
    
    For i=0 to max 
    	For j=i+1 to max 
    		If ArrFileCreated(i)>ArrFileCreated(j) then
    			TempVar1=ArrFileCreated(i)
    			TempVar2=ArrFileName(i)
    			ArrFileCreated(i)=ArrFileCreated(j)
    			ArrFileCreated(j)=TempVar1
    			ArrFileName(i)=ArrFileName(j)
    			ArrFileName(j)=TempVar2
    		end if
    	next 
    next
    David Parkes
    Nuclear Internet - Windows Web Hosting
    http://www.nuclearinternet.com

  5. #5
    Original Gangster silver trophy Thing's Avatar
    Join Date
    Oct 2000
    Location
    Philadelphia, PA
    Posts
    4,708
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I would imagine they are comparable, but the array might actually be faster.

    Here is the code:
    Code:
    Set objRS = Server.CreateObject("ADODB.Recordset")
    objRS.CursorLocation = 3 ' adUseClient
    objRS.Fields.Append "Name", 200, 50 ' adVarChar
    objRS.Fields.Append "created", 7 ' adDate
    objRS.Open
     
    imagespath = "C:\inetpub\wwwroot\imagez\"
     
    Dim FSObj, FOlderObj, FileCollObj
    Set FSObj = Server.CreateObject("Scripting.FileSystemObject")
     
    Set FolderObj = FSObj.GetFolder(imagespath)
    Set FolderCollObj = FolderObj.SubFolders
    For Each item in FolderCollObj
    Set SubFolderObj = FSObj.GetFolder(FolderObj & "/" & item.name)
    objRS.AddNew
    objRS("Name") = item.name
    objRS("created") = SubFolderObj.DateCreated
    Next
    objRS.Sort = "created DESC"
    objRS.MoveFirst
    Do UNTIL objRS.EOF
    response.write "<a href=""gallery.asp?gallery=" & objRS(0) & """>"
    response.write objRS(0) & "&nbsp;<BR><BR></A>"
    objRS.movenext
    Loop
    This is kind of gritty code, written about 3 years ago. You can see here:

    Code:
    For Each item in FolderCollObj
    Set SubFolderObj = FSObj.GetFolder(FolderObj & "/" & item.name)
    objRS.AddNew
    objRS("Name") = item.name
    objRS("created") = SubFolderObj.DateCreated
    Next
    That I am looping through the file collection and adding records into the disconnected recordset. Then:

    Code:
    objRS.Sort = "created DESC"
    objRS.MoveFirst
    I resort the recordset before displaying. I would stick with your array code, looks more efficient to me.

  6. #6
    SitePoint Addict
    Join Date
    Oct 2003
    Location
    Tenerife, Spain / UK
    Posts
    329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for you help Thing, nice solution. But I just realised this whole exercise is irrelevant.

    I don't need to sort my FileNames in Arrays at all, they are already in order!
    My filenames are all numbered sequentially 123.xml; 124.xml 125.xml etc... Consequently they are read into my initial Array in order of oldest to most recent. All I need to do is loop through the array backwards.

    Code:
    For x = max to 1 step -1
    'Display ArrFileName(x)
    Next
    I'd initially ruled this out fearing that 1.xml might be sorted along with 10.xml eg:

    1.xml
    10.xml
    11.xm
    2.xml
    3.xml
    4.xml
    5.xml
    6.xml
    7.xml
    8.xml
    9.xml

    But Windows actually gets it right by itself. Realising this has saved me a massive headache and made my code a lot more efficient.

    Lesson Learned: If you want to pull files out of a folder in the order they were created, name your files using sequential numbers. (Thank god I already had this in place.)
    David Parkes
    Nuclear Internet - Windows Web Hosting
    http://www.nuclearinternet.com


Bookmarks

Posting Permissions

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