Accessing collections with FSO

  • Thread starter Thread starter Steve
  • Start date Start date
S

Steve

I have a folder object whose associated files collection I can iterate
through using a "for each / next" loop to retrieve all the file names
individually. I cannot however directly access the n'th item in the files
collection? Here is my code so far;

' create FSO & folder objects
Dim objFSO
objFSO = Server.CreateObject("Scripting.FileSystemObject")
Dim objFld 'folder object &
objFld = objFSO.GetFolder(Server.MapPath("/test"))

' now directly access file # 4 & return file name
Return objFld.Files.Item(4).Name

This code snippet gives me a "parameter is incorrect" on the "return" line.

Any ideas? Thanks

Steve
 
hiya...

for starters i would use the My.Computer.FileSystem object, because the
Scripting.FileSystemObject is extremely slow in .NET.

' make a read-only collection:
Dim files As ReadOnlyCollection(Of String)

' use My.Computer.FileSystem.GetFiles to return a ReadOnlyCollection of all
files in a folder.
' you can change the 2nd and 3rd parameters to suit:
files = My.Computer.FileSystem.GetFiles( folderpath,
FileIO.SearchOption.SearchAllSubDirectories, anyWildcards)

' Display the FIFTH item:
msgbox files.item(4)


HTH
 
Hi Steve,

It seems that Files.Item() property requires name of the file passed,
bot it's number, e.g.:

~
MessageBox.Show(fso.GetFolder("C:\").Files.Item("BOOTLOG.TXT").Path)
~

Indeed, what is file #4? The don't have any order.


Also, I strongly advice you to use System.IO.Directory class for such
kind of jobs.

Hope this helps,
Roman
 
Hi, and thanks for the response & ideas.

I seem only able to "Dim" my collections as "ReadOnlyCollectionBase" &

your "files = My.Computer...." assignment gives me the "Name 'my' is not
declared" error.

I probably should mention that I am trying to implement this functionality
into a web control.

Any other ideas? Thanks, Steve
 
what version of .NET are you using??



Steve said:
Hi, and thanks for the response & ideas.

I seem only able to "Dim" my collections as "ReadOnlyCollectionBase" &

your "files = My.Computer...." assignment gives me the "Name 'my' is not
declared" error.

I probably should mention that I am trying to implement this functionality
into a web control.

Any other ideas? Thanks, Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top