SitePoint Sponsor |
|
User Tag List
Results 1 to 6 of 6
-
Dec 29, 2005, 07:18 #1
- 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
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
-
Dec 29, 2005, 10:02 #2
- 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.
-
Dec 29, 2005, 11:18 #3
- 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.
"Does this napkin smell like chloroform?"
...now with SnapFoo!
My Blog | My Twitter | My Company | SitePoint Podcast
*** Matt Mullenweg on the SitePoint Podcast ***
-
Dec 29, 2005, 11:35 #4
- 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
-
Dec 30, 2005, 13:29 #5
- 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) & " <BR><BR></A>" objRS.movenext Loop
Code:For Each item in FolderCollObj Set SubFolderObj = FSObj.GetFolder(FolderObj & "/" & item.name) objRS.AddNew objRS("Name") = item.name objRS("created") = SubFolderObj.DateCreated Next
Code:objRS.Sort = "created DESC" objRS.MoveFirst
"Does this napkin smell like chloroform?"
...now with SnapFoo!
My Blog | My Twitter | My Company | SitePoint Podcast
*** Matt Mullenweg on the SitePoint Podcast ***
-
Jan 3, 2006, 11:06 #6
- 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
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.)
Bookmarks