Print Page | Close Window

Using: Scripting.FileSystemObject

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Classic ASP Discussion
Forum Description: Discussion on Active Server Pages (Classic ASP).
URL: https://forums.webwiz.net/forum_posts.asp?TID=10522
Printed Date: 31 March 2026 at 11:59am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Using: Scripting.FileSystemObject
Posted By: Nathan
Subject: Using: Scripting.FileSystemObject
Date Posted: 19 May 2004 at 2:19pm

I am trying to list the contents of each folder using the following code. It works fine, but I would like to be able to access certain elements of oFolder.Files (see code below). Is this created as an array - if so, how could I list for example file 50-100 only?

Thanks - Nathan

Set oFSO = CreateObject("Scripting.FileSystemObject")

CurrentPath = Server.MapPath("/message_boards/posts/")

Set oFolder = oFSO.GetFolder(CurrentPath)
Set oFolderContents = oFolder.Files

For Each oFileItem in oFolder.Files

        witeout file name

next




Replies:
Posted By: dpyers
Date Posted: 19 May 2004 at 8:26pm

Don't think you can index it as in the past, I've seen routines to load the filelist into an array. For your situation, you may want to try something like this

Quote Set oFSO = CreateObject("Scripting.FileSystemObject")

CurrentPath = Server.MapPath("/message_boards/posts/")
Set oFolder = oFSO.GetFolder(CurrentPath)
Set oFolderContents = oFolder.Files

dim i
i = 0
For Each File in oFolder.Files
     i = i+1
     If i > 49 and i < 101 then
          Response.Write file.Name  & "<br />"
     end if
next

Set oFolder = Nothing
Set oFolderContents = Nothing
Set oFSO = Nothing

You'd probably want to check oFolderContents.count to make sure it was =/> than 50 and =/< 100 before doing the loop. - something like:

Quote Dim starti
Dim endi
If oFolderContents.count < 50 then
    starti = oFolderContents.count
else
    starti = 49
End If
If oFolderContents.count < 100 then 
    endi = oFolderContents.count
Else
    endi = 101
End If

Then in the For loop, change 49 and 101 to starti and endi respectively.



-------------

Lead me not into temptation... I know the short cut, follow me.


Posted By: Nathan
Date Posted: 20 May 2004 at 3:03pm

Perfect - thanks for that DP - exactly what I needed.

Cheers

Nathan

http://www.agni.gr - www.agni.gr

 



Posted By: Nathan
Date Posted: 23 May 2004 at 9:12am

I have just run into a  small problem. The files are always sorted Alphabetically whereas I need them sorted with the latest first. Is there a way of doing this?

Thanks again.

Nathan

 

Set oFSO = CreateObject("Scripting.FileSystemObject")

CurrentPath = Server.MapPath("/message_boards/posts/")
Set oFolder = oFSO.GetFolder(CurrentPath)
Set oFolderContents = oFolder.Files



Posted By: dpyers
Date Posted: 23 May 2004 at 10:54am

There's no direct way to sort using FSO. The two indiect ways that are often used are to put the info into an array and sort the array - for dates, you'll need a two dimensional array, or put the info into a recordset and sort the record set.

You also have to figure out which FSO date you want to use. for an FSO File object, you have:
    .DateCreated
  .DateLastModified
  .DateLastAccessed

AspFaqs had some tutorials on array sorting. A google for asp fso sort would turn up more.



-------------

Lead me not into temptation... I know the short cut, follow me.



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net