Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem accessing folder using Scripting.FileSystemObject 2

Status
Not open for further replies.

Brando32

Programmer
Jul 25, 2002
11
US
Hey VB Gurus .... this code snippet yields an error and I am baffled as to why. As I type the line "oFolder.Files.Item(i).Name" each part of it appears for you to select so I know it is valid syntactically, but it yields an error when run. I tried starting i at 1 and at 0, but still yields error - and yes there are files in that folder. Any help would be greatly appreciated. Thanks

Dim fs As Scripting.FileSystemObject
Dim oFolder As Scripting.Folder
Set fs = New Scripting.FileSystemObject

sTPath = "\some valid path"
Set oFolder = fs.GetFolder(sTPath)

for i = 0 to oFolder.Files.count
msgbox oFolder.Files.Item(i).Name
next i

yields ....

Error 5: Invalid procedure call or argument.

 
Where does the error occur, and what message do you get? As it stands, the line : for i = 0 to oFolder.Files.count should be for i = 0 to oFolder.Files.count -1

This is because the array is from 0 to count-1.

BB
 
THanks for the response BiggerBrother ....

The error is:
Error 5: Invalid procedure call or argument

And it occurs on the line:
msgbox oFolder.Files.Item(i).Name

Even if the upper bound of the loop is wrong, it still should have executed until that point. It won't even execute once .....
 
This works...

I suspect that the problem is soemting to do with the item property of the files collection.

Code:
Dim fs As Scripting.FileSystemObject
Dim oFolder As Scripting.Folder
Dim sTPath As String
Dim objFile As File
Dim objFileCol As Files


Set fs = New Scripting.FileSystemObject

sTPath = "c:\"
Set oFolder = fs.GetFolder(sTPath)
Set objFileCol = oFolder.Files


For Each objFile In objFileCol
  MsgBox objFile.Name
Next

so i've drawn some code that uses the for ..each construct, which is useful for interating through collections!

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
's actually not looking for an index in this case, its the 0 it doesn't like. It's looking for a "Key" which has to be the name itself. To get the names use

Dim fs As Scripting.FileSystemObject
Dim oFolder As Scripting.Folder
Dim a As File
Set fs = New Scripting.FileSystemObject

sTPath = "C:\"
Set oFolder = fs.GetFolder(sTPath)

For Each a In oFolder.Files
MsgBox (a.Name)
Next a
 
>Ya beat me by that much....

But it was enough.... [lol]

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Thanks Matt and Rdrosk ....

But I still wonder why "oFolder.Files.Item(i).Name" won't work? I did re-write the code as you said ... a for each loop and it does work.
 
It is because an integer is not allowed as an index for Item(), its looking for a string.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top