StormX02
08-31-2003, 03:48 PM
How do I scan a directory for a list of folders? Can someone give me a hint as to how to how I should go about doing this?
Eg. C:\Lala\ has 10 folders. I have a listbox and I need it to list all 10 folders' names.
Any ideas?
OnErr0r
08-31-2003, 03:51 PM
Call Dir$ in a loop with the vbDirectory flag. You'll find an example in the help file under Dir$ function.
StormX02
08-31-2003, 03:52 PM
I was thinking about that, but wouldn't it return the first folder and only the first folder?
OnErr0r
08-31-2003, 03:55 PM
No, it will return all folders and files in the directory you specify.
StormX02
08-31-2003, 04:04 PM
No, it will return all folders and files in the directory you specify.
uhh problem.. I did X = Dir(C:\Windows, vbDirectory) and MsgBox X returned "Windows"
I tried X = Dir(C:\Windows\, vbDirectory), and MsgBox X returned "."
AHHHHH!!!!!!
OnErr0r
08-31-2003, 04:06 PM
Yes, and you continue to call it in a loop until nothing is returned. Both files and directories will be returned. You use GetAttr to differenciate directories from files.
Please look at the example I mentioned at:
http://msdn.microsoft.com/library/en-us/vbenlr98/html/vafctdirx.asp
StormX02
08-31-2003, 04:12 PM
ahh thanks...
what does the "MyName = Dir" do though? the one right before Loop
they say it gets next entry but if MyName is only used to store stuff then what impact could "MyName = Dir" possibly have on anything?
OnErr0r
08-31-2003, 04:15 PM
It's calling the function once to start the enumeration. Each time Dir$ is called you get a new filename/directory until there is nothing left, in which case "" is returned.
StormX02
08-31-2003, 04:23 PM
Dim FolderName As String
Private Sub Form_Load()
FolderName = Dir("C:\Windows\", vbDirectory)
Do While FolderName <> ""
If FolderName <> "." And FolderName <> ".." Then
If (GetAttr("C:\Windows\" & FolderName) And vbDirectory) = vbDirectory Then
MsgBox FolderName
End If
End If
Loop
End Sub
When I do that, I get an infinite loop. when I use "C:\Windows" instead, I get "file not found"
what's wrong?! =(
Thinker
08-31-2003, 04:58 PM
You aren't using Dir inside your loop.
Dim FolderName As String
Private Sub Form_Load()
FolderName = Dir("C:\Windows\", vbDirectory)
Do While FolderName <> ""
If FolderName <> "." And FolderName <> ".." Then
If (GetAttr("C:\Windows\" & FolderName) And vbDirectory) = vbDirectory Then
MsgBox FolderName
End If
End If
FolderName = Dir() '<- This line is missing
Loop
End Sub
StormX02
08-31-2003, 07:57 PM
ahh k now it works thanks