Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

For Each objFile in objFolder.Files

Status
Not open for further replies.

charanch

Programmer
Jan 3, 2004
55
US
hello everyone, question about the following code. Is there a way to sort these files by date descending? i.e. the files that display from this code are weekly timesheets and named by the date, so january's would be 1-1-2004 but since they are sorting by name, the most recent is at the bottom. Any thoughts would be appreciated.

Code:
Dim objFSO
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

Dim objFolder
Set objFolder = objFSO.GetFolder("D:\[URL unfurl="true"]www\test\updates")[/URL]

Dim objFile
For Each objFile in objFolder.Files
Response.Write "<IMG SRC=""../images/more_arrows.gif"">&nbsp;<a href='" & objFile.Name & "' target=new>" & objFile.Name & "</a>"
Next
 
I would write them to a recordset, then sort them however you wanted.

Here is an old example of one I did. I created a sub and passed to it:

Code:
Sub FillRS(ByVal objFolderContents, ByRef objRS)
  
  ' Start at the beginning
   objRS.CursorLocation = adUseClient               
  ' Create Attributes
   objRS.Fields.Append "FName", adVarChar, 255
   objRS.Fields.Append "FPath", adVarChar, 255
   objRS.Fields.Append "FType", adVarChar, 255
   objRS.Fields.Append "LastModified", adDate
   objRS.Open

   For each item in objFolderContents
  ' Populate the RecordSet
     objRS.AddNew
     objRS.Fields("FName").value = item.Name
     objRS.Fields("FPath").Value = item.path
     objRS.Fields("FType").value = item.Type
     objRS.Fields("LastModified").value = item.DateLastModified
     objRS.Update
     objRS.MoveNext
  Next
End Sub
 
Write them to a 2-dimensional array (the second dimension being dateCreated), sort the array by the date and then write the output...

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

zen.gif
 
Thanks to both of you. Ignoreme, I don't know how to use that method or either one of the methods for that matter. I'm not overly swift when it comes to FSO stuff and wouldn't even know where to start. I looked up the two dimensional array stuff and tried to figure out how to apply it to what I need but I'm giving up. The files will have to stay unsorted. Many thanks to both of you.
 
Nevermind. I found the following code that seems to be working fine. Many thanks for your help.
Code:
dim fso : set fso = server.createobject("scripting.filesystemobject")
dim folder : set folder = fso.getFolder("YOURFILEPATH")
if folder.files.count > 0 then
  dim rs : set rs = server.createobject("ADODB.Recordset")
  with rs
    .cursorlocation = 3
    .cursortype = 2
    .fields.append "file_name",200,255,&H00000020
    .fields.append "file_date",7,32,&H00000020
    .open
    dim x : for each x in folder.files
      .addnew
      .fields("file_name") = x.name
      .fields("file_date") = x.datecreated
      .update
    next
    .sort = "file_date DESC"
    .movefirst
    do until .eof
      response.write(.fields("file_name"))
      .movenext
    loop
    .close
  end with
  set rs = nothing
else
  response.write("this folder contains no files")
end if
set folder = nothing : set fso = nothing
 
that'll be writing them to a recordset then, as suggested by ignoreme.

obviously an appropriate handle [lol]

Chris.

Indifference will be the downfall of mankind, but who cares?
A website that proves the cobblers kids adage.
Nightclub counting systems

So long, and thanks for all the fish.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top