Accessing collection element.

Hi there,

***********************
        Dim fso As Object
        Dim fld As Object
        Dim subfs As Object
        Dim subf As Object
       
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set fld = fso.GetFolder(path)
        Set subfs = fld.SubFolders
        Set subf = subfs.Item(2)
***********************

I get an error on the last line, "Invalid procedure call or argument"...
LVL 3
Unimatrix_001Asked:
Who is Participating?
 
NightmanConnect With a Mentor CTOCommented:
You have to access it via the key name, not the index, so
set  subf = subf.Item("foldername")
It seems a little pointless, as most of the time you are creating the collection to get the folder names.

You can still iterate through the collection with a for ... each statement

  For Each subf In subfs
     MsgBox subf.Name
  Next
0
 
Unimatrix_001Author Commented:
What I'm after is getting hold of the last folder in that collection, is there a tidy way to do it without a for loop?
0
 
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
>What I'm after is getting hold of the last folder in that collection, is there a tidy way to do it without a for loop?
the "last" means the list needs to be ordered. however, the collections are not ordered in a guaranteed way, so you need
1) to specify the "order"
2) order by that property
3) get the last from there.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
NightmanCTOCommented:
Without knowing the key name, not that I know of.

I agree with you, it seems kind of dumb that you can't get it by index and having to know the key name to reference the object (why else would you be iterationg through in the first place, if not to get the name!).
0
 
Unimatrix_001Author Commented:
angelIII: Could you expand on that please, are those methods I can use etc... ?
0
 
Unimatrix_001Author Commented:
"Without knowing the key name, not that I know of."
Hmm, okay. Although yes, seems incredibly silly not getting an element by index!
0
 
Dany BalianConnect With a Mentor CTOCommented:
try this

        Dim fso As Object
        Dim fld As Object
        Dim subfs As Object
        Dim subf As Object, subfol As Object
       
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set fld = fso.GetFolder("C:\")
        Set subfs = fld.SubFolders
        For Each subf In subfs
            Set subfol = subf
        Next
       
        MsgBox subfol.Name  '<< this will give u the last item in the list...


hope this helps..

dan

0
 
Unimatrix_001Author Commented:
mrdany: Thanks, although what I'm after is:

"is there a tidy way to do it without a for loop?"

Although, I'll wait for AngelIII to reply, and then I'll probably split the points. :)
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
>"is there a tidy way to do it without a for loop?"
no.
0
 
Dany BalianCTOCommented:
sorry unimatrix, i didnt read the comments.. i just read the main question and answered...
my bad!!
0
 
Unimatrix_001Author Commented:
Hrm, okay... Thanks any everybody for your contributions. Points split. :)
0
 
Unimatrix_001Author Commented:
mrdany: Np mate. ;)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.