Avatar billede para7 Nybegynder
25. marts 2004 - 11:31 Der 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

        Response.write objFileItem.DateLastModified & " - " & objFileItem.name & "<br>" & vbCrLf
   
    Next
    Set objFSO = Nothing
%>
</body>
</html>
Avatar billede wolfgang Praktikant
25. marts 2004 - 11:38 #1
Hej Para7,

Jeg har netop lige kodet selv samme og fandt denne side meget anvendlig.
http://www.sourcecodecorner.com/articles/asp/directoryview.asp

Håber det kan hjælpe.

MVH
Wolfgang
Avatar billede wolfgang Praktikant
25. marts 2004 - 11:41 #2
Det er en lidt omstændig løsning, men den virker 100%.

Kan varmt anbefales.
Avatar billede wolfgang Praktikant
25. marts 2004 - 11:42 #3
Her er min kode:

<%

'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


'Give variables some initial values
thisfile = lcase(request.servervariables("path_translated"))
filecount = 0
foldercount = 0
totalfilesize = 0
arrayposvalue = 0
dir = request.querystring("dir")
sortby = request.querystring("sortby")

'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>&nbsp;&nbsp; " & formatnumber(dirarray(2,writecount), 0) & " bytes</font></td><td align=right><font size=2>&nbsp;&nbsp;&nbsp; " & datepart("d", dirarray(3,writecount)) & "/" & datepart("m", dirarray(3,writecount)) & "/" & datepart("yyyy", dirarray(3,writecount)) & " </font></td><td align=right><font size=2>&nbsp; " & 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>&nbsp;&nbsp;&nbsp; " & datepart("d", foldarray(3,writecount)) & "/" & datepart("m", foldarray(3,writecount)) & "/" & datepart("yyyy", foldarray(3,writecount)) & " </font></td><td align=right><font size=2>&nbsp; " & 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>&nbsp;&nbsp;&nbsp; " & datepart("d", foldarray(3,writecount)) & "/" & datepart("m", foldarray(3,writecount)) & "/" & datepart("yyyy", foldarray(3,writecount)) & " </font></td><td align=right><font size=2>&nbsp; " & 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>&nbsp;&nbsp;</td><td align=right>&nbsp;&nbsp;&nbsp; (Oprettet d. " & filevalue.datelastmodified & ")</td><td align=right>&nbsp; </td></tr>"
              end if
      next
end if


%>

</table>
Avatar billede para7 Nybegynder
25. marts 2004 - 19:37 #4
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
Avatar billede Ny bruger Nybegynder

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.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester