Avatar billede hundevennen Nybegynder
10. juli 2007 - 15:13 Der er 14 kommentarer og
1 løsning

hjælp med FSO objekt

Jeg bruger nedenstående til at vise alt, men hvis jeg nu kun vil vise de sidste 10 nyeste filer hva så, hvordan gør jeg det?

UPLOAD_PATH    = Server.MapPath("resultatlister")

    Dim g_oFso, g_oFolder, g_oFile
        Set g_oFso    = Server.CreateObject("Scripting.FileSystemObject")
        Set g_oFolder    = g_oFso.getFolder(UPLOAD_PATH)

    If (Request.ServerVariables("REQUEST_METHOD") = "POST") Then
        Dim g_oUpload
            Set g_oUpload    = get_upload_files()

        Dim fpos, fcontent
            fcontent    = g_oUpload("upload").Item("content")
        Set g_oFile    = g_oFso.CreateTextFile(UPLOAD_PATH & "\" & extract_filename(g_oUpload("upload").Item("filename")))
        For fpos = 1 to LenB(fcontent)
            g_oFile.Write chr(AscB(MidB(fcontent, fpos, 1)))
        Next
        g_oFile.Close: Set g_oFile    = Nothing

        Response.Redirect Request.ServerVariables("SCRIPT_NAME")
    End If

    Response.Write "<html><body><table class='startliste'>"
   
    Response.Write "<tr><td><hr></td></tr>"
    For Each g_oFile In g_oFolder.Files
      Response.Write "<tr><td><a href=""resultatlister/" & g_oFile.Name & """ target=_blank>" & g_oFile.name & "</a></td></tr>"
    Next
    Response.Write "<tr><td><hr></td></tr>"
   
    Response.Write "</table></body></html>"


For Each g_oFile In g_oFolder.Files
    if right(g_oFile.name,3) = "asp" then
    Response.write ""
    else
      Response.Write "<tr><td><a href=""resultatlister/" & g_oFile.Name & """ target=_blank>" & g_oFile.name & "</a></td></tr>"
    end if
    Next
   

%>
Avatar billede fennec Nybegynder
10. juli 2007 - 15:21 #1
Der bliver du nød til at loade dine informationer over i et array eller recordset. Jeg bruger altid et recordset, da det giver flere muligheder. Her har du et link:
http://www.roubaixinteractive.com/PlayGround/FSO/Sort_Folder_Contents.asp
Avatar billede hundevennen Nybegynder
11. juli 2007 - 09:06 #2
Hej Fennec - jeg har lidt problemer med at fange det rigtige bibliotek
i denne linie Set objFolder = objFSO.GetFolder(theFolder)
har jeg ændret til Set objFolder = objFSO.GetFolder(".") som skulle gøre objfolder til den folder selve denne fil ligger i og liste de pdf filer jeg har liggende der???
Avatar billede fennec Nybegynder
11. juli 2007 - 09:47 #3
Du skal have en mappath ind:
Set objFolder = objFSO.GetFolder(server.mappath("."))
Avatar billede hundevennen Nybegynder
11. juli 2007 - 09:58 #4
det kan jeg jo gøre, men jeg fik indsat den rigtige sti som d:/xxx/xxx/ og det virker - jeg får listet filerne - nu skal jeg bare lave noget med at vælge TOP 10
Avatar billede fennec Nybegynder
11. juli 2007 - 10:03 #5
Top 10 kan du klare via udskriften, når de vel og mærke er sorteret rigtig med Sort kaldet. Du udskriver jo bare de 10 første:

count = 0
while not rs.eof and count<10
  ..Udskriv..
  count = count + 1
wend
Avatar billede hundevennen Nybegynder
11. juli 2007 - 10:57 #6
jaja - nemt for dig - jeg er nået ned til denne linie
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
og jeg får listet alt hvad der er i biblioteket - nu skal jegha filtreret nogle filtyper fra og kun listet de 10 nyeste filer????
Avatar billede hundevennen Nybegynder
11. juli 2007 - 11:10 #7
du har hjulpet mig tidligere med samme problem med at filtrere nogle filtyper fra
if right(g_oFile.name,3) = "asp" or right(g_oFile.name,3)="css" or right(g_oFile.name,3)="jpg" then og det virker fint - og det er samme asp side, men der er efterhånden bare for mange filer i biblioteket og derfor skal jeg bare ha de 10 nyeste
Avatar billede fennec Nybegynder
11. juli 2007 - 11:13 #8
Du burde via Filter kunne frasortere/vælge hvilke filertyper du vil se:
rsFso.Filter = "Type='gif' or Type='jpg' or Type='doc'"

Sort sortere:
rsFSO.Sort = "DateCreated DESC"

Også bruger du bare count metoden fra forrige post til at vise de 10 første rækker.
Avatar billede hundevennen Nybegynder
11. juli 2007 - 11:15 #9
du blir nødt til at skære det ud i pap hvis du vil og har tid, - please
Avatar billede fennec Nybegynder
11. juli 2007 - 11:36 #10
'Indsæt ALLE filer i recordsettet:
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
'Filtrer resultater (fjern asp, css og jpg filer):
rsFSO.Filter = "Type<>'asp' and Type<>'css' and Type<>'jpg'"

'Sorter efter dato:
rsFSO.Sort = "DateCreated DESC"

'Flyt til første række:
rsFSO.MoveFirst()

'Udskriv 10 første:
count = 0
While Not rsFSO.EOF and count<10
%>
    <p><%= rsFSO("Name").Value %> | <%= rsFSO("Type").Value %></p>
<%
  'and let's move to the next record
  rsFso.MoveNext()
  count = count + 1
Wend
Avatar billede hundevennen Nybegynder
11. juli 2007 - 13:57 #11
Fennec - jeg har prøvet at tilpasse med dit, - men jeg får hele tiden fejl om manglende end eller functionm - dette virker ihverfaldt, men det henter jo altså kun alt indhold
<%
'**********
'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
  With rsFSO.Fields
    .Append "Name", adVarChar, 200
    .Append "Type", adVarChar, 200
    .Append "DateCreated", adDate
    .Append "DateLastAccessed", adDate
    .Append "DateLastModified", adDate
    .Append "Size", adInteger
    .Append "TotalFileCount", adInteger
  End With
  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 = "DateCreated DESC "

  '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
%>
Avatar billede fennec Nybegynder
11. juli 2007 - 14:22 #12
Det er sikkert File.Type, som har drillet. Den henter nemlig ikke asp, gif osv men "ASP dokument", "Billed fil".

Dette har jeg testet og det virker. Fjerne zip og asp dokumenter. Du kan selv rette Filter til:

<%
'**********
'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
  With rsFSO.Fields
    .Append "Name", adVarChar, 200
    .Append "Type", adVarChar, 200
    .Append "DateCreated", adDate
    .Append "DateLastAccessed", adDate
    .Append "DateLastModified", adDate
    .Append "Size", adInteger
    .Append "TotalFileCount", adInteger
  End With
  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
      if instr(File.Name,".") then
          rsFSO("Type") = mid(File.Name,instrrev(File.Name,".")+1)
      else
          rsFSO("Type") = ""
      end if
      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 = "DateCreated DESC "
  rsFSO.Filter = "Type <> 'zip' and Type <> 'asp'"

  '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(".")

'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.
  count = 0
  While Not rsFSO.EOF and count < 10
%>
    <p><%= rsFSO("Name").Value %> | <%= rsFSO("Type").Value %></p>
<%
  'and let's move to the next record
    rsFso.MoveNext()
    count = count + 1
  Wend
 
  'finally, close out the recordset
  rsFSO.close()
  Set rsFSO = Nothing
%>
Avatar billede fennec Nybegynder
11. juli 2007 - 14:24 #13
Du skal lige huske at rette:
Dim strFolder : strFolder = Server.MapPath(".")

til:
Dim strFolder : strFolder = Server.MapPath("\PlayGround\FSO\stuff\")
Avatar billede hundevennen Nybegynder
12. juli 2007 - 10:02 #14
som sædvane gør du det igen - læg et svar og tak for hjælpen
Avatar billede fennec Nybegynder
12. juli 2007 - 10:05 #15
.o) <-- One Eyed Jack
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