25. marts 2004 - 11:31Der er
1 kommentar og 3 løsninger
Sortering med filsystemet
Hvordan er det mon man kan sortere henholdsvis efter dato og navn? Man kan vel ikke som i SQL skrive order by ... Jeg har her en testside:
<html> <head> <title>Filsystem test for sortering</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <% strPhysicalPath = Server.MapPath("testbilleder") Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFolder = objFSO.GetFolder(strPhysicalPath) Set objFolderContents = objFolder.Files For Each objFileItem in objFolderContents
AI bliver først for alvor en del af arbejdet, når teknologien integreres i den måde, vi arbejder på. At have adgang til AI betyder ikke nødvendigvis, at medarbejderne er klar til at bruge den.
'Initialise all variables dim objfile dim myfilename dim myfoldername dim parentfolder dim filevalue dim filecount dim foldercount dim totalfilesize dim thisfile dim thisfilename Dim objFSO Dim strCurrentFolder Dim objFolder dim linkfilename dim linkfoldername dim dirarray dim foldarray Dim arrayposvalue dim writecount dim dir dim newsort dim sortby dim imgvalue dim j dim i dim temp dim tempcol dim colno
'If dir and sortby contain rubbish, then give them a default value. if (dir <> "descn" and dir <> "ascn") then dir = "descn" end if
if (sortby <> "filename" and sortby <> "filesize" and sortby <> "filedate") then sortby = "" end if
'Toggle the dir field, and also change the image if dir = "descn" then dir = "ascn" imgvalue = "/graphics/sortdown.gif" elseif (dir = "ascn" or dir = "") then dir = "descn" imgvalue = "/graphics/sortup.gif" end if
'loop through all the files and folders, putting the values into arrays Sub findfilesindirectory(objFolder) 'Now, use a for each...next to loop through the Files collection For Each objFile in objFolder.Files if (lcase(objfile.path) <> thisfile) then 'Get the local path and filename myfilename = lcase(objfile.path) set filevalue = objfso.getfile(myfilename) 'Replace local path with a . to use current directory myfilename = replace(myfilename, strcurrentfolder, "") 'replace any \ with / myfilename = replace(myfilename, "\", "/") 'Ensure spaces are filled in correctly. Fixes URL link for Netscape linkfilename = replace(myfilename, " ", "%20") 'Fill the array with the file details. dirarray(0,arrayposvalue) = linkfilename dirarray(1,arrayposvalue) = myfilename dirarray(2,arrayposvalue) = filevalue.size dirarray(3,arrayposvalue) = filevalue.datelastmodified dirarray(4,arrayposvalue) = "file" 'count the total filesize for all the files to display at the bottom of the page. totalfilesize = totalfilesize + filevalue.size end if arrayposvalue = arrayposvalue + 1 Next
arrayposvalue = 0 Dim objSubFolder 'Do the same as above, but now for folders. For Each objSubFolder in objFolder.SubFolders myfoldername = lcase(objsubfolder) set foldervalue = objfso.getfolder(myfoldername) 'We only need the folder name, not the whole path to the folder myfoldername = replace(myfoldername, strcurrentfolder, "") 'Fix the URL link for spaces in folder names. Netscape also needs a / (added in the array) linkfoldername = replace(myfoldername, " ", "%20") 'Build the array for the folders. foldarray(0,arrayposvalue) = linkfoldername & "/" foldarray(1,arrayposvalue) = myfoldername foldarray(2,arrayposvalue) = "" foldarray(3,arrayposvalue) = foldervalue.datelastmodified foldarray(4,arrayposvalue) = "folder" arrayposvalue = arrayposvalue + 1 Next End Sub
'Open the filesystem so we can get the file details Set objFSO = Server.CreateObject("Scripting.FileSystemObject") 'strCurrentFolder = lcase(server.mappath("."))
Set objFolder = objFSO.GetFolder(strCurrentFolder) strcurrentfolder = strcurrentfolder & "\"
'Get the filename for use in the sorting links thisfilename = replace(thisfile, strcurrentfolder, "")
'Get file and folder counts. We knock one off of file count, because we don't want to display this file in the folder. filecount = objfolder.files.count -1 foldercount = objfolder.subfolders.count
'Size the arrays redim dirarray(4, filecount) redim foldarray(4, foldercount)
'Run the routine above to find all the files and folders findfilesinDirectory objFolder
%>
<!-- display the sort headings -->
<%
'Sort the array, depending on the column and direction
if sortby <> "" and dir = "ascn" then if sortby = "filename" then colno = 0 if sortby = "filesize" then colno = 2 if sortby = "filedate" then colno = 3
'Sort the file array, ascending for j = 0 to (filecount -1) for i = 0 to (filecount -1) if dirArray(colno,i) < dirArray(colno,i+1) then for tempcol = 0 to 4 temp=dirArray(tempcol,i+1) dirArray(tempcol,i+1)=dirArray(tempcol,i) dirArray(tempcol,i)=temp next end if next next
'Sort the folder array, ascending for j = 0 to (foldercount -1) for i = 0 to (foldercount -1) if foldArray(colno,i) < foldArray(colno,i+1) then for tempcol = 0 to 4 temp=foldArray(tempcol,i+1) foldArray(tempcol,i+1)=foldArray(tempcol,i) foldArray(tempcol,i)=temp next end if next next
end if
if sortby <> "" and dir = "descn" then if sortby = "filename" then colno = 0 if sortby = "filesize" then colno = 2 if sortby = "filedate" then colno = 3
'Sort the file array, descending for j = 0 to (filecount -1) for i = 0 to (filecount -1) if dirArray(colno,i) > dirArray(colno,i+1) then for tempcol = 0 to 4 temp=dirArray(tempcol,i+1) dirArray(tempcol,i+1)=dirArray(tempcol,i) dirArray(tempcol,i)=temp next end if next next
'Sort the folder array, descending for j = 0 to (foldercount -1) for i = 0 to (foldercount -1) if foldArray(colno,i) > foldArray(colno,i+1) then for tempcol = 0 to 4 temp=foldArray(tempcol,i+1) foldArray(tempcol,i+1)=foldArray(tempcol,i) foldArray(tempcol,i)=temp next end if next next
end if
'If the sort is descending, display the files at the top, the subfolders below. if dir = "descn" then for writecount = 0 to filecount if dirarray(4, writecount) = "file" then Response.Write "<tr><td><a href=""" & dirarray(0,writecount) & """><img src=""Images/icon_Pdf.gif"" align=""absmiddle"" border=0 alt=""" & dirarray(1,writecount) & """></a> <a href=""" & dirarray(0,writecount) & """>" & dirarray(1,writecount) & "</a></td><td align=right><font size=2> " & formatnumber(dirarray(2,writecount), 0) & " bytes</font></td><td align=right><font size=2> " & datepart("d", dirarray(3,writecount)) & "/" & datepart("m", dirarray(3,writecount)) & "/" & datepart("yyyy", dirarray(3,writecount)) & " </font></td><td align=right><font size=2> " & timevalue(dirarray(3,writecount)) & " </font></td></tr>" end if next
for writecount = 0 to foldercount if foldarray(4, writecount) = "folder" then Response.Write "<tr><td><a href=""" & foldarray(0,writecount) & """><img src=""/graphics/folder_closed.gif"" border=0 alt=""" & foldarray(1,writecount) & """></a> <a href=""" & foldarray(0,writecount) & """>" & foldarray(1,writecount) & "</a></td><td></td><td align=right><font size=2> " & datepart("d", foldarray(3,writecount)) & "/" & datepart("m", foldarray(3,writecount)) & "/" & datepart("yyyy", foldarray(3,writecount)) & " </font></td><td align=right><font size=2> " & timevalue(foldarray(3,writecount)) & " </font></td></tr>" end if next end if
'If the sort is ascending, then display the subfolders at the top, the files below. if dir = "ascn" then for writecount = 0 to foldercount if foldarray(4, writecount) = "folder" then Response.Write "<tr><td><a href=""" & foldarray(0,writecount) & """><img src=""/graphics/folder_closed.gif"" border=0 alt=""" & foldarray(1,writecount) & """></a> <a href=""" & foldarray(0,writecount) & """>" & foldarray(1,writecount) & "</a></td><td></td><td align=right><font size=2> " & datepart("d", foldarray(3,writecount)) & "/" & datepart("m", foldarray(3,writecount)) & "/" & datepart("yyyy", foldarray(3,writecount)) & " </font></td><td align=right><font size=2> " & timevalue(foldarray(3,writecount)) & " </font></td></tr>" end if next
for writecount = 0 to filecount if dirarray(4, writecount) = "file" then Response.Write "<tr><td><a href=""" & dirarray(0,writecount) & """><img src=""Images/icon_Pdf.gif"" align=""absmiddle"" border=0 alt=""" & dirarray(1,writecount) & """></a> <a href="""& CFolder & dirarray(1,writecount) & """>" & dirarray(1,writecount) & "</a></td><td align=right> </td><td align=right> (Oprettet d. " & filevalue.datelastmodified & ")</td><td align=right> </td></tr>" end if next end if
Tak wolfgang Jeg kan bruge det script du henviste til til noget andet, men min opgave er ikke løst endnu, så jeg har oprettet et nyt spørgsmål: http://www.eksperten.dk/spm/481998
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.