PDA

View Full Version : how i can sort files in folder


bmwmpower
04-21-2003, 04:20 PM
i have code to display all files in the folder so i need view this files sorted by date
this is my code
'=======================================
Function ShowFileList2(folderspec)
Dim fso, f, fc, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(folderspec)
Set fc = f.Files
For Each f1 in fc
img="": edt=0
GetExt = fso.GetExtensionName(folderspec & "\" & f1.name)
if lcase(getext) = "mdb" then img = "imgs\access.jpg"
if lcase(getext) = "asp" then img = "imgs\asp.jpg":edt=1
if lcase(getext) = "xls" then img = "imgs\excel.jpg"
if lcase(getext) = "exe" then img = "imgs\exe.jpg"
if lcase(getext) = "frm" then img = "imgs\frm.jpg"
if lcase(getext) = "htm" or lcase(getext) = "html" then img = "imgs\htm.jpg":edt=1
if lcase(getext) = "txt" then img = "imgs\text.jpg":edt=1
if lcase(getext) = "pdf" then img = "imgs\pdf.gif"
if lcase(getext) = "zip" then img = "imgs\winzip.gif"
if lcase(getext) = "doc" then img = "imgs\word.jpg"
if lcase(getext) = "ppt" then img = "imgs\power.gif"
if img = "" then img = "imgs\no.jpg"
'====================================================
dim conn , sql , rs
Set conn = Server.CreateObject("ADODB.connection")
conn.Open "DBQ=" & Server.Mappath("SEARCH.MDB") & ";Driver={Microsoft Access Driver (*.mdb)};"
sql=" select owner ,filname,DateAdded from owner ORDER BY DateAdded DESC"
Set Rs = Server.CreateObject("ADODB.Recordset")
Rs.Open SQL, conn
'=========================================================
s = s & "<tr>"
s = s & "<td class='dataTD' width='5%' height='15' align='center'><b><font face='Verdana' size='2'><a href='" & c & "\" & f1.name & "' target='_Blank'><img src='" & img & "'alt='View File' border='0'></font></b></td>"
s = s & "<td class='dataTD' width='150' height='10' align='left'><b><font face='Verdana' size='1'>" & f1.name & "</font></b></td>"
Rs.Filter = "filname='" & f1.Name & "'"
IF NOT Rs.EOF THen
s = s & "<td class='dataTD' width='90' align='center' height='10'><b><font size='1' face='Verdana'>"& rs("owner")&"</font></b></td>"
s = s & "<td class='dataTD' width='80' align='left' height='10'><b><font size='1' face='Verdana'>"& f1.size &"</font></b></td>"
s = s & "<td class='dataTD' width='125' align='center' height='10'><b><font size='1' face='Verdana'>" & rs("DateAdded") & "</font></b></td>"

ELSE
s = s & "<td class='dataTD' width='80' align='left' height='10'><b><font size='1' face='Verdana'>"& f1.size &"</font></b></td>"
s = s & "<td class='dataTD' width='125' align='center' height='10'><b><font size='1' face='Verdana'>" & f1.datecreated & "</font></b></td>"
END IF
s = s & "<td class='dataTD' width='25' colspan='1' height='10' align='center'><b><font face='Verdana' size='2'><a href='cut.asp?c=" & c + "\" & "&f=" & c + "\" + f1.name & + "&d=" & d & "'><img src='imgs\cut.gif' alt='Cut File' border='0'></a></font></b></td>"
'if rs("owner")= session("temp") then s = s & "<td class='dataTD' width='25' height='10' align='center'><b><font face='Verdana' size='1'><a href='delfile2.asp?c=" & c & + "&d=" & d & "&f=" & c + "\" + f1.name & + "&f2=" & f1.name &"'><img src='imgs\del.jpg' alt='delete File' border='0'></a></font></b></td>" ELSE
's = s & "<td class='dataTD' width='25' height='10' align='center'><b><font face='Verdana' size='1'><img src='imgs\del2.jpg' alt='delete File' border='0'></font></b></td>"
'END IF
if d= "admin" then s = s & "<td class='dataTD' width='25' height='10' align='center'><b><font face='Verdana' size='1'><a href='delfile2.asp?c=" & c & + "&d=" & d & "&f=" & c + "\" + f1.name & + "&f2=" & f1.name &"'><img src='imgs\del.jpg' alt='delete File' border='0'></a></font></b></td>" else
s = s & "</tr>"
s = replace(s,"\\","\")
Next
Response.Write s
End Function
%>

ecnarongi
04-21-2003, 04:36 PM
I got this from the web, but I looked at it and looks like it should be what you are looking for. The basis is it takes all info from the folder creates a recordset and stores it there, then sorts it.

<%
'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
rsFSO.Fields.Append "Name", adVarChar, 200
rsFSO.Fields.Append "Type", adVarChar, 200
rsFSO.Fields.Append "DateCreated", adDate
rsFSO.Fields.Append "DateLastAccessed", adDate
rsFSO.Fields.Append "DateLastModified", adDate
rsFSO.Fields.Append "Size", adInteger
rsFSO.Fields.Append "TotalFileCount", adInteger
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

%>

Let me know if this helps. :thumbsup:

bmwmpower
04-22-2003, 02:24 PM
sorry i don't understand so can u tell me what i must do in my code to make this sort