|
-
Sep 12th, 2002, 08:22 AM
#1
Thread Starter
Bouncy Member
Recursive sub dirs using Dir()
hello all.
ive done a search on this one and come up with various results but nothing solid. most of them are using the api, i just want to write a simple recursive function to retrieve all subdirs and their subdirs etc and return their paths in a string array, thats all....
only problem is that you cant use the Dir() function in a recursive...
i also dont want to use the filesytemobject as its way too much overhead...
any advice would be lovely..
-
Sep 12th, 2002, 10:33 AM
#2
You can use the Dir function you just have to be a bit clever with it.
To look at a recursive object you need to act recursive youself.
Create 1 function that you pass a path to, that then looks at the suggest path and gets a list of all the files. Do a quick test to see if the file is a folder, if so put into a Folders array, else put into a file array.
Then go through the folder array and call yourself to get the data back (the recursion), it will auto-follow all sub dirs and come back with an entire list.
Compress at end, and send back to caller.
All you have to do to use it, is call this or the like
strArray() = funcGetFiles( "C:")
VB Code:
Dim strDocArray(0) as string
Dim strFolderArray(0) as string
Function funcGetFiles(FilePath as String) as String()
strThing = Dir(FilePath & "\*")
Do until strThing = ""
If strThing ISFOLDER 'FORGET CODE OFF TOP OF HEAD then
strFolderArray(Ubound(strFolderArray)) = strThing
Redim strFolderArray(Ubound(strFolderArray)+1)
Else
strDocArray(Ubound(strDocArray)) = strThing
Redim strDocArray(Ubound(strDocArray)+1)
End If
strThing = Dir
Loop
'Now go through the folders
Dim FinalList() as String
Dim strFolderPaths() as string
Dim intIndex as Integer
For intIndex = 0 to Ubound(strFolderArray) - 1
'The all important call to yourself, dont mess up or you will get an overflow !
strFolderPaths= funcGetFiles( strFolderArray(intIndex) )
Dim intIndex2 as Integer
For intIndex2 = 0 to Ubound(strFolderPaths) - 1
FinalList(Ubound(FinalList)) = strFolderPaths(intIndex2)
Next
Next
'Add the current list of files and folders as well !!
blah blah FinalList 'ETC ETC
Return FinalList
End Function
PS: This should be enough for you to go on, please dont make me do the whole thing .
PPS. check for files called . and .. and removed them, Dir returns the dummy checks sometimes.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|