Oct 31st, 2003, 02:15 AM
#1
Thread Starter
Frenzied Member
get every folder on the drive! recurse sub folders directories
nice n easy..
first click project -> references, and check Microsoft Scripting Runtime
Win 98 SE and before will need a newer version of scrrun.dll
VB Code:
Public Function GetFolders(sStartFolder As String) As String
On Error Resume Next
Dim fso As New FileSystemObject
Dim f As Folder
Dim fldr As Folder
Set fldr = fso.GetFolder(sStartFolder)
GetFolders = fldr.Path & vbCrLf
If fldr.SubFolders.Count > 0 Then
For Each f In fldr.SubFolders
GetFolders = GetFolders & GetFolders(f.Path)
Next
End If
If Err.Number = 92 Then GetFolders = vbNullString
'windows did not allow the program to look inside this folder, so ignore it completely (ie c:\system volume information in win2000)
End Function
note - the code got an edit recently... the following posts may or may not make sense
Last edited by dis1411; Nov 5th, 2004 at 03:30 PM .
Oct 31st, 2003, 09:46 AM
#2
Frenzied Member
can't you simply use the MS-DOS comand: tree
type:
tree /a >tree.txt
when you're on the C:\ in MSDOS prompt, then (using windows) open C:\tree.txt
Oct 31st, 2003, 05:04 PM
#3
Thread Starter
Frenzied Member
well good luck converting that output into something your program can use
Nov 12th, 2003, 11:39 AM
#4
New Member
Could this code be alter to where it would read in every file in a directory?
I don't sufer from insanity, I enjoy it.
Nov 12th, 2003, 01:19 PM
#5
Thread Starter
Frenzied Member
yes
add a file list (file1) to the project and set its visible property to false
VB Code:
Dim PathArr() As String
Dim TempDir As String
Private Sub Command1_Click()
TempDir = "C:\"
PathArr() = Split(GetFolders(TempDir), vbCrLf)
For subs = 1 To UBound(PathArr)
File1.Path = PathArr(subs)
If File1.ListCount > 0 Then
For files = 0 To File1.ListCount - 1
'add PathArr(subs) & "\" & File1.List(files) to something (textbox etc)
Next
End If
Next
End Sub
Nov 12th, 2003, 04:10 PM
#6
New Member
Sweet that helps alot big TY.
I don't sufer from insanity, I enjoy it.
Feb 1st, 2004, 12:31 AM
#7
Frenzied Member
Re: get every folder on the drive! recurse sub folders directories
Originally posted by dis1411
note - the returned string will contain an extra vbCrLf at the beginning.. its not that bad cuz most likely youll split the string into an array and you can start at 1 instead of 0 when processing it
Just change your code one tiny bit to fix that, why don't you...
VB Code:
Private Function Getfolders(sStartFolder As String) As String
Dim fso As New FileSystemObject
Dim f As Folder
Dim fldr As Folder
Set fldr = fso.GetFolder(sStartFolder)
Getfolders = fldr.Path & VbCrLf
If fldr.SubFolders.Count > 0 And Getfolders <> "" Then
For Each f In fldr.SubFolders
Getfolders = Getfolders & Getfolders(f.Path)
Next f
End If
End Function
Feb 1st, 2004, 04:05 AM
#8
Thread Starter
Frenzied Member
yeah, but then youre left with an extra vbCrLf at the end..
the when you split the result into an array it's going to be the same size either way, but the vbCrLf will be at the beggining or the end
Feb 3rd, 2004, 05:24 PM
#9
yeah, but then youre left with an extra vbCrLf at the end..
Then at the end do :
VB Code:
Getfolders = Left$(Getfolders, Len(Getfolders) - 2)
Has someone helped you? Then you can Rate their helpful post.
Feb 4th, 2004, 07:40 PM
#10
Frenzied Member
Feb 4th, 2004, 08:26 PM
#11
Thread Starter
Frenzied Member
Originally posted by manavo11
Then at the end do :
VB Code:
Getfolders = Left$(Getfolders, Len(Getfolders) - 2)
obviously, yes
the point was that something extra has to be done either way
Feb 5th, 2004, 08:31 AM
#12
Feb 5th, 2004, 10:55 AM
#13
Frenzied Member
Feb 5th, 2004, 02:29 PM
#14
Thread Starter
Frenzied Member
Originally posted by manavo11
Yeah, well, I don't see a problem in doing something extra since in both ways it would be needed anyway...
perhaps you took me wrong. i wasnt complaining that something extra has to be done, i was stating it.
if anyone were to have a 'problem' with taking out the vbcrlf, then their program would also problems
Feb 5th, 2004, 03:37 PM
#15
Apr 5th, 2004, 08:30 PM
#16
Fanatic Member
There is a better way of doing this that does not include a reference to the scripting runtime lib. I once found it on these forums but I have now lost the idea.
Basically it is also a recorsive function, the first part populates an array of folders.For each folder, the list of files are returned. Next folder type of style...
If any1 has it then cool - please post the code or link here
Apr 6th, 2004, 04:52 AM
#17
Apr 6th, 2004, 05:53 AM
#18
Fanatic Member
Thanks manavo
you hit the nail on my head!!
I
Apr 6th, 2004, 02:51 PM
#19
Thread Starter
Frenzied Member
for some reasion when i posted this i liked the FSO way over the Dir way.. i can't remember why anymore
Apr 6th, 2004, 04:07 PM
#20
Apr 20th, 2004, 11:35 AM
#21
Hyperactive Member
I seem to have found that Dir() seems to leave the directories open after you use it in Windows XP, so I changed all my Dirs to Use the FindFirst/FindNext/FindClose API Calls.....
Attached Files
Jun 16th, 2012, 03:09 AM
#22
New Member
Re: get every folder on the drive! recurse sub folders directories
very goooooooooood
thanks
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