How to show a Directory Tree in VB6?

How do I show a directory tree dialog screen so that the user can choose a file?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You're contradicting yourself. Do you want the user to choose a folder or a file?
Bob LearnedCommented:
To browse for a file you can use a common dialog box.

To browse for a folder you could use:

Here's some easy to use code for displaying a directory browsing
window. This allows a user to select a directory.

Private Type BrowseInfo
  hWndOwner      As Long
  pIDLRoot       As Long
  pszDisplayName As Long
  lpszTitle      As Long
  ulFlags        As Long
  lpfnCallback   As Long
  lParam         As Long
  iImage         As Long
End Type

'Browsing for directory.
Private Const BIF_RETURNONLYFSDIRS = &H1      'For finding a folder to
start document searching
Private Const BIF_DONTGOBELOWDOMAIN = &H2     'For starting the Find
Private Const BIF_STATUSTEXT = &H4

Private Const BIF_BROWSEFORCOMPUTER = &H1000  'Browsing for Computers.
Private Const BIF_BROWSEFORPRINTER = &H2000   'Browsing for Printers
Private Const BIF_BROWSEINCLUDEFILES = &H4000 'Browsing for Everything

Private Const MAX_PATH = 260

Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal _
lpString1 As String, ByVal lpString2 As String) As Long

Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As _
BrowseInfo) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList
As _
Long, ByVal lpBuffer As String) As Long

Public Function BrowseForFolder(hWndOwner As Long, sPrompt As String)
As String

   'Opens the system dialog for browsing for a folder
   Dim iNull As Integer
   Dim lpIDList As Long
   Dim lResult As Long
   Dim sPath As String
   Dim udtBI As BrowseInfo

  With udtBI
     .hWndOwner = hWndOwner
     .lpszTitle = lstrcat(sPrompt, "")

  End With

  lpIDList = SHBrowseForFolder(udtBI)
  If lpIDList Then
     sPath = String$(MAX_PATH, 0)
     lResult = SHGetPathFromIDList(lpIDList, sPath)
     Call CoTaskMemFree(lpIDList)
     iNull = InStr(sPath, vbNullChar)
     If iNull Then
        sPath = Left$(sPath, iNull - 1)
     End If
  End If

  BrowseForFolder = sPath

End Function

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Use the ShowOpen method of the CommonDialog control.

From VB help:

Private Sub Command1_Click()
    ' Set CancelError is True
    CommonDialog1.CancelError = True
    On Error GoTo ErrHandler
    ' Set flags
    CommonDialog1.Flags = cdlOFNHideReadOnly
    ' Set filters
    CommonDialog1.Filter = "All Files (*.*)|*.*|Text Files" & _
    "(*.txt)|*.txt|Batch Files (*.bat)|*.bat"
    ' Specify default filter
    CommonDialog1.FilterIndex = 2
    ' Display the Open dialog box
    ' Display name of selected file
    MsgBox CommonDialog1.filename
    Exit Sub
    'User pressed the Cancel button
    Exit Sub
End Sub
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!


you can select a directoy using fso as well...don't have the code but it's in help.
The browser(win-explorer type) control works as well.
What's fso?
rrr333222Author Commented:
It's a folder that I want to select. I tried the code you provided but nothing is happening. I am assuming that I should be using/calling the BrowseForFolder() function, but does it make a diference what the parameteres are?
rrr333222Author Commented:
I check FSO but did not find a way to use it to get a Tree of the drive/paths, etc.
Bob LearnedCommented:
You can call the BrowseForFolder routine like this:

FileName = BrowseForFolder(Form1.hWnd, "Select Folder")
Check out

you'll want the ccrpbds6.dll It's beautiful.
Again, what is FSO?
Bob LearnedCommented:
FSO is the FileSystemObject in the Scripting library
Aha. Thank you Learned One.
rrr333222Author Commented:
I am trying to browse for a Folder, sorry if I was not clear in the question. I wouldn't mind giving you the points as well, but since I can only give to one person I have to give it to theLearnedOne
rrr333222Author Commented:
I am trying to browse for a Folder, sorry if I was not clear in the question. I wouldn't mind giving you the points as well, but since I can only give to one person I have to give it to theLearnedOne
rrr333222Author Commented:
i increased the points, as i see that this was more involved than  i expected it to be.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.