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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Referencing a specific folder in a folder colection

Status
Not open for further replies.

snotmare

Programmer
Jan 15, 2004
262
0
0
US
Greetings!

For the record, I have already checked multiple sources (including the Microsoft website) and cannot find an answer to this question. When all else fails, I know someone in the tek-tips community will have what I'm looking for!!

You typically see all file/folder processing as follows:
Code:
Set objFileSys = CreateObject("Scripting.FileSystemObject")

For Each objFolder In objFileSys.GetFolder("C:\").SubFolders
    Msgbox objFolder.Path
Next

What I want to do, is reference a specific element in the collection by doing something like this...
Code:
Set objFileSys = CreateObject("Scripting.FileSystemObject")
Set objFolders = objFileSys.GetFolder("C:\").SubFolders

Msgbox objFolders(3).Path

I do know that this works...
Code:
Set objFileSys = CreateObject("Scripting.FileSystemObject")
Set objFolders = objFileSys.GetFolder("C:\").SubFolders

Msgbox objFolders.Item("Program Files")
Msgbox objFolders("Program Files")

But this does not...
Code:
Set objFileSys = CreateObject("Scripting.FileSystemObject")
Set objFolders = objFileSys.GetFolder("C:\").SubFolders

Msgbox objFolders.Item(3)

Nor anything in this list...
Code:
Msgbox objFolders("3")
Msgbox objFolders.Item("3")
Msgbox objFolders.Key(3)
Msgbox objFolders.Key("3")
I do not want to load each file path in an internal array just so I can reference by number.

Any ideas of how I can reference a specific folder in the objFolders collection by simply referencing the element by number???

He who has knowledge spares his words, and a man of understanding is of a calm spirit. Even a fool is counted wise when he holds his peace; when he shuts his lips, he is considered perceptive. - King Solomon
 
I don't know of a way to do it, but I'm curious to know what the need to do it is. Could you explain why you need to do this? I can't think think of any situation where someone would need to and I am always curious to see the way that other people solve their problems.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Sure! Here's an explanation of what I'm trying to do...

I'm creating an internal web page for some clients. It's nothing to fancy, but the goal is to replace an older application that is very slow and confusing to the users.

The main reason the old application is so slow, is because it loops through lots of files in different directories, and populates a list box. I've figgured out how I can speed up this process (or at least from the user's perspective), but putting this loop logic into HTML timer (or interval) events. That way, the application loads the list box without locking the user from doing something else. So basically, I'm trying to use this timer event as my loop, so that every interval of the event is one iteration in my loop.

That's where the problem comes in. With HTML interval events, the event is basically called at every interval. For each interval, I need to know where the last interval left off with in my file or folder collection. I was hoping to use some sort of global counter.

Sorry for the long explanation. Here is some psudo code that might make it a bit clearer...
Code:
intState = 0
intTimer = Window.SetInterval("intervalEvent", 1)

sub intervalEvent()
    select case intState
        case 0
           'initiate global FSO
           'build folder collection
           intState = 1
           intFolder = 0
        
        case 1
           'process folderCollection(intFolder)
           'intFolder = intFolder + 1
           if intFolder > ubound(folderCollection) then
               intState = 2
           end if

        case 2
           'clean up
end sub

I hope that makes sense. Any ideas of how I might achieve the same thing using timers would be appreciated!

He who has knowledge spares his words, and a man of understanding is of a calm spirit. Even a fool is counted wise when he holds his peace; when he shuts his lips, he is considered perceptive. - King Solomon
 
FYI,

javascript has a solution for me that I found at:

I would much rather code in VB, but this will work. Thanks to anyone who may have ideas!

He who has knowledge spares his words, and a man of understanding is of a calm spirit. Even a fool is counted wise when he holds his peace; when he shuts his lips, he is considered perceptive. - King Solomon
 
it seems vbscript does't support real collections, so there is no "key()" method.

my closest solution could be filtered "for each ...":

cnt=0
for each f in sf
if cnt>= cnt_last then
'procede with next elements ...
cnt_last=cnt_last+1
end if
if cnt_last>cnt_max then 'exit loop, return later to it
exit for
end if
cnt=cnt+1
next

so, this reentrant piece of code should skip over previous elements until desired one found.
 
Thanks picado!

VBScript does seem to be limited in this area, especially if I'm going for performance.

Good idea though!

He who has knowledge spares his words, and a man of understanding is of a calm spirit. Even a fool is counted wise when he holds his peace; when he shuts his lips, he is considered perceptive. - King Solomon
 
built in folder processor is most suitable, but for general purpose, here is another variation for vbscript:

function collection_item(col_mycollection,index)
dim i,c
i=1
for each c in col_mycollection
if i=index then
collection_item=c
exit for
end if
i=i+1
next
end function

call for collection(index):

item=collection_item(my_collection,index)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top