How to get all the sub folders under a directory vb6
Under this folder
mstrOwnerFolder holds: C:\Documents and Settings\All Users.WINDOWS\Documents\RoofCalculator\Williams 1247 RiverBend rd
C:\Documents and Settings\All Users.WINDOWS\Documents\RoofCalculator\Williams 1247 RiverBend rd\Roofing Bids
C:\Documents and Settings\All Users.WINDOWS\Documents\RoofCalculator\Williams 1247 RiverBend rd\Fencing Bids
C:\Documents and Settings\All Users.WINDOWS\Documents\RoofCalculator\Williams 1247 RiverBend rd\LanndScaping Bids
C:\Documents and Settings\All Users.WINDOWS\Documents\RoofCalculator\Williams 1247 RiverBend rd\Gardner Bids
C:\Documents and Settings\All Users.WINDOWS\Documents\RoofCalculator\Williams 1247 RiverBend rd\New Gutter Bids
I am trying to get these sub folders
Roofing Bids
Fencing Bids
LanndScaping Bids
Gardner Bids
New Gutter Bids
Had a problem with FSO few yrs ago so i would rather not use it
Tried this to no avail
Code:
Sub AddFoldersToCbo()
Dim MyFile, MyPath, MyName
MyPath = mstrOwnerFolder
MyName = Dir(MyPath, vbDirectory)
Do While MyName <> ""
If MyPath = vbDirectory Then
Debug.Print MyName
End If
MyName = Dir
Loop
End Sub
how to do this ?
Re: How to get all the sub folders under a directory vb6
Re: How to get all the sub folders under a directory vb6
Wow lots of Variants there. Is there a reason?
In any case I assume MyPath is a (Variant) String. Why would it ever make sense for it to be equal to vbDirectory, an enum value equal to 16?
The Dir() example in the VB6 manual does exactly what you want, almost suspiciously so right down to the embarrassing clunkiness of using Variants. Is your clipboard broken? ;)
Re: How to get all the sub folders under a directory vb6
dilettante you talking about?
Code:
' Display the names in C:\ that represent directories.
MyPath = "c:\" ' Set the path.
MyName = Dir(MyPath, vbDirectory) ' Retrieve the first entry.
Do While MyName <> "" ' Start the loop.
' Ignore the current directory and the encompassing directory.
If MyName <> "." And MyName <> ".." Then
' Use bitwise comparison to make sure MyName is a directory.
If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
Debug.Print MyName ' Display entry only if it
End If ' it represents a directory.
End If
MyName = Dir ' Get next entry.
Loop
I could only get it to subfolders if the whole path plus extention was in the "Mypath" variable.
Re: How to get all the sub folders under a directory vb6
Quote:
Originally Posted by
Nightwalker83
Thanks Nightwalker83, but that's overkill and wants a pattern
Re: How to get all the sub folders under a directory vb6
I would advise that you avoid using that stupid Dir function for all but the simplest tasks. Don't know why MS kept that old QuickBasic relic in the language. You should instead use something based on the FindFirstFile API. I'll have to go through my old VB6 projects to see if I have an implementation for subdirectory enumeration lying around.
Re: How to get all the sub folders under a directory vb6
Quote:
Originally Posted by
Niya
I would advise that you avoid using that stupid Dir function for all but the simplest tasks. Don't know why MS kept that old QuickBasic relic in the language. You should instead use something based on the FindFirstFile API. I'll have to go through my old VB6 projects to see if I have an implementation for subdirectory enumeration lying around.
thanks for adding that.didn't realize this was such a big thing.
My app creates the directories so i have decided to store the paths in a string array and save that
Re: How to get all the sub folders under a directory vb6
It isn't a big thing at all, and the Dir() function is nothing but a thin wrapper around the ANSI FindFirstFile() and FindNextFile() calls. There is little to be gained by calling them directly.
Slow down, take a breath, and debug your code. This is simple stuff people do all the time and it seems extremely strange that it is giving you such fits.
Re: How to get all the sub folders under a directory vb6
Quote:
Originally Posted by
Nightwalker83
dilettante you talking about?
Code:
' Display the names in C:\ that represent directories.
MyPath = "c:\" ' Set the path.
MyName = Dir(MyPath, vbDirectory) ' Retrieve the first entry.
Do While MyName <> "" ' Start the loop.
' Ignore the current directory and the encompassing directory.
If MyName <> "." And MyName <> ".." Then
' Use bitwise comparison to make sure MyName is a directory.
If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
Debug.Print MyName ' Display entry only if it
End If ' it represents a directory.
End If
MyName = Dir ' Get next entry.
Loop
I could only get it to subfolders if the whole path plus extention was in the "Mypath" variable.
Yes, that example works fine. Just make sure your MyPath value ends in a "\" character or it thinks you are asking about the contents of the folder above that one.
Re: How to get all the sub folders under a directory vb6
Quote:
Originally Posted by
dilettante
the Dir() function is nothing but a thin wrapper around the ANSI FindFirstFile() and FindNextFile() calls. There is little to be gained by calling them directly.
I beg to differ. Try writing a recursive search to enumerate a directory tree and you'd see just how retarded Dir really is. Unlike the APIs it calls, you cannot obtain a search handle and as such Dir only maintains one search handle at a time. This is the Achilles Heel of the Dir function. And we haven't even begun to talk about multi-threaded or server applications. Imagine a server trying to serve multiple requests from different clients(possibly on multiple threads), all of which need to perform a quick look-up of the file system. Dir certainly cannot be used in this case unless you synchronize access to the filesystem yourself so Dir wouldn't be used by more than one thread at a time. That ridiculous relic has caused me all kinds of pain before I stopped using it. I only way I used it before I stopped using VB6 was to check if a file existed. Seemed the only thing it was good for. For anything that involved complex searching where I needed control over multiple handles, I used the APIs directly.
Re: How to get all the sub folders under a directory vb6
You don't need (and don't want) to retain those handles. That leads to tremendous overhead, and is an especially poor idea for a server.
It is a simple matter to cache folder namess and then traverse each of those after completing the list at the current level. Trivial, and easily done with excellent performance as demonstrated in the CodeBank thread already linked in post #2 here.
There is no reason to send somebody off on a wild goose chase when they can't get even simple things they copy/pasted to work.
Re: How to get all the sub folders under a directory vb6
In this case the question is only about getting folders that are in a given folder. Should be very simple using Dir$()
In fact the code in the OP is almost correct as is probably just missing the \ on the path
1 Attachment(s)
Re: How to get all the sub folders under a directory vb6
This is my RecDir class, and there http://www.vbforums.com/showthread.p...-Folder-Select is an example of using it. Also there are three types of sorting.
In a form
Code:
Public WithEvents yourDir As RecDir
Private Sub Form_Load()
Set yourDir = New RecDir
yourDir.IncludedFolders = True
yourDir.Nofiles = True
yourDir.LevelStop = 3
yourDir.Dir2 "c:\"
End Sub
Private Sub yourDir_DirFinished()
Dim i As Long
Caption = yourDir.listcount
' i start from 1 to exclude .. or from 0 to included it
' If we set the topfolder then the ".." label is excluded automatic therefor we start always from 0
' but this is needed only in a file selector
For i = 1 To yourDir.listcount - 1
List1.AddItem Mid$(yourDir.List(i), 2)
Next i
End Sub
Re: How to get all the sub folders under a directory vb6
Quote:
Originally Posted by
dilettante
It is a simple matter to cache folder namess and then traverse each of those after completing the list at the current level. Trivial, and easily done with excellent performance as demonstrated in the CodeBank thread already linked in post #2 here.
I've actually done it that way and its no where near as elegant or simple as the recursive method. Of course its really a matter of preference. Whatever works for you I guess.
Quote:
Originally Posted by
dilettante
There is no reason to send somebody off on a wild goose chase when they can't get even simple things they copy/pasted to work.
Fair enough. I just hate hate that Dir function. I had to say something. I was strongly compelled to chime in.
Re: How to get all the sub folders under a directory vb6
Niya,
I found this class impressive, but am unable to use it on a server as this class seems to be written for a client.
I have now loaded it as a class in my server side code in my project group.
How can I use your class on the server by just giving a pathname and a pattern from my client?
Thanks
PK
Re: How to get all the sub folders under a directory vb6
Yes, I am aware this thread's last reply is from 2019. But hoping someone can still use this:
Code:
'This module contains this program's main code.
Option Explicit
Private Declare Function SafeArrayGetDim Lib "Oleaut32.dll" (ByRef saArray() As String) As Long
'This procedure returns the directories contained by the current directory.
Private Function GetDirectories() As String()
On Error GoTo ErrorTrap
Dim Directories() As String
Dim Item As String
Item = Dir$("*.*", vbArchive Or vbDirectory Or vbHidden Or vbReadOnly Or vbSystem)
Do Until Item = vbNullString
If (GetAttr(Item) And vbDirectory) = vbDirectory Then
If Not (Item = "." Or Item = "..") Then
ChDir Item
ChDir ".."
If SafeArrayGetDim(Directories()) = 0 Then
ReDim Directories(0 To 0) As String
Else
ReDim Preserve Directories(LBound(Directories()) To UBound(Directories()) + 1) As String
End If
Directories(UBound(Directories())) = Item
End If
End If
NextDirectory:
If Item = vbNullString Then Exit Do
Item = Dir$(, vbArchive Or vbDirectory Or vbHidden Or vbReadOnly Or vbSystem)
Loop
GetDirectories = Directories()
Exit Function
ErrorTrap:
Resume NextDirectory
End Function
'This procedure writes the directory tree starting at the specified root to the specified file.
Private Sub GetDirectoryTree(Root As String, FileH As Long)
Dim Directories() As String
Dim DirectoryIndex() As Long
ChDrive Left$(Root, InStr(Root, ":"))
ChDir Root
ReDim DirectoryIndex(0 To 0) As Long
DirectoryIndex(UBound(DirectoryIndex())) = 0
Do
DoEvents
Do
Directories() = GetDirectories()
If SafeArrayGetDim(Directories()) = 0 Then Exit Do
ChDir Directories(DirectoryIndex(UBound(DirectoryIndex())))
ReDim Preserve DirectoryIndex(LBound(Directories()) To UBound(DirectoryIndex()) + 1) As Long
Loop
Do
Print #FileH, CurDir$()
ChDir ".."
Directories() = GetDirectories()
If UBound(DirectoryIndex()) = LBound(DirectoryIndex()) Then Exit Sub
ReDim Preserve DirectoryIndex(LBound(DirectoryIndex()) To UBound(DirectoryIndex()) - 1) As Long
If DirectoryIndex(UBound(DirectoryIndex())) < UBound(Directories()) Then
DirectoryIndex(UBound(DirectoryIndex())) = DirectoryIndex(UBound(DirectoryIndex())) + 1
Exit Do
End If
Loop
Loop
End Sub
'This procedure scans through a directory tree.
Private Sub Main()
On Error GoTo ErrorTrap
Dim FileH As Long
Dim OutputFile As String
Dim Root As String
ChDrive Left$(App.Path, InStr(App.Path, ":"))
ChDir App.Path
Root = Left$(App.Path, InStr(App.Path, ":")) & "\"
OutputFile = App.Path
If Not Right$(OutputFile, 1) = "\" Then OutputFile = OutputFile & "\"
OutputFile = OutputFile & "Tree.txt"
Root = InputBox$("Start at:", , Root)
If Root = vbNullString Then Exit Sub
OutputFile = InputBox$("Output file: ", , OutputFile)
If OutputFile = vbNullString Then Exit Sub
FileH = FreeFile()
Open OutputFile For Output Lock Read Write As FileH
GetDirectoryTree Root, FileH
Close FileH
EndRoutine:
Exit Sub
ErrorTrap:
MsgBox Err.Description, vbExclamation
Resume EndRoutine
End Sub
@niya - It hardly matters especially after 6 years, but the DIR function appears to have been a late addition to Quick Basic. It only appeared with version 7.1.
Re: How to get all the sub folders under a directory vb6
Quote:
Originally Posted by
Peter Swinkels
@niya - It hardly matters especially after 6 years, but the DIR function appears to have been a late addition to Quick Basic. It only appeared with version 7.1.
Not sure where you got that notion but I am pretty sure it was around long before that. I just looked to confirm and it is definitely in VB-DOS which is in line with Quick Basic 4.5 which btw is the last version of Quick Basic and still available on the MS web site. 7.1 sounds like you are talking about the product they called PDS which I think did go up to 7.1 but given its apparent roots in Quick Basic it likely had the Dir$() function in earlier versions.
Re: How to get all the sub folders under a directory vb6
[QUOTE=Peter Swinkels;5486595]Yes, I am aware this thread's last reply is from 2019. But hoping someone can still use this:
[code]
Thanks Peter, I can still use it in an upgrade to my .dll
PK
Re: How to get all the sub folders under a directory vb6
Quote:
Originally Posted by
DataMiser
Not sure where you got that notion but I am pretty sure it was around long before that. I just looked to confirm and it is definitely in VB-DOS which is in line with Quick Basic 4.5 which btw is the last version of Quick Basic and still available on the MS web site. 7.1 sounds like you are talking about the product they called PDS which I think did go up to 7.1 but given its apparent roots in Quick Basic it likely had the Dir$() function in earlier versions.
Hello DataMiser,
I got the notion by checking my copy of Quick Basic 7.1 (PDS?) and 4.5. The latter doesn't have the DIR function as far as I can tell. And yes, Visual Basic for DOS has that function but I isn't that separate from Quick Basic?
yours,
Peter Swinkels
@peekay: you're welcome!
Re: How to get all the sub folders under a directory vb6
I have QB4.5 pro here somewhere but has been years since I did anything with it. I have the manual for VBDos setting on my shelf so I did look it up in there.
To the best of my knowledge the Quick Basic product only goes to 4.5. After ward they continued what they called PDS [Microsoft Basic Professional Development System] which goes up to 7.1
It would appear both predate VB-Dos so I can't be sure now without looking at the actual 4.5 version which is not installed on any of my current PCs and I have no idea where the installer is.
That said after doing a quick search it does appear that Dir$() was added late. The only reference I can find in older forms of Basic is the Files function.
Re: How to get all the sub folders under a directory vb6
Quote:
Originally Posted by
DataMiser
I have QB4.5 pro here somewhere but has been years since I did anything with it. I have the manual for VBDos setting on my shelf so I did look it up in there.
To the best of my knowledge the Quick Basic product only goes to 4.5. After ward they continued what they called PDS [Microsoft Basic Professional Development System] which goes up to 7.1
It would appear both predate VB-Dos so I can't be sure now without looking at the actual 4.5 version which is not installed on any of my current PCs and I have no idea where the installer is.
That said after doing a quick search it does appear that Dir$() was added late. The only reference I can find in older forms of Basic is the Files function.
Hi DataMiser,
I didn’t know there was a pro version of Quick Basic 4.5. If that is the case you could be right after all.
Peter