10. juli 2007 - 15:13Der 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 "<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
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???
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
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????
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
'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
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 %>
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 %>
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.