Results 1 to 5 of 5

Thread: Get a directory

  1. #1

    Thread Starter
    Hyperactive Member rockies1's Avatar
    Join Date
    Jul 1999
    Location
    Stuck at work
    Posts
    375

    Question

    I need to have a user navigate to a directory and then have all the subdirectories in that directory listed in a list box.

    I can do the list box part if I provide the directory explicitly, but I want the user to naviagate there and choose it at run time....

    Can this be done with a CommonDialog? If so, how? I'm stumped. It won't seem to return anything to me unless I choose a file.

    Thanks!!
    Morgan
    [email protected] - Home
    [email protected] - Work
    Using VB6 SP6 but trying to learn VB2005EE

  2. #2
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    Drop this in your declaration of your form with a listbox

    To call it: Call p_Get_SubDirectories(Dir1.Path)


    Code:
    Option Explicit
    
    Sub p_Get_SubDirectories(strPath As String)
      'PURPOSE: If Path lacks a "\", add one to the end
      If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
    
      'PURPOSE: Find file
      Dim strReturn As String
      strReturn = Dir(strPath & "*.*", vbDirectory)
      
      Dim strSubDir As String
      Do Until strReturn = ""
          'PURPOSE: Don't process "." and "..", they aren't real files
          If strReturn <> "." And strReturn <> ".." Then
              'PURPOSE: Check to see if it is a Directory
              If FileLen(strPath & strReturn) = 0 Then    'Directory is consist of 0 byte
                  If GetAttr(strPath & strReturn) = vbDirectory Then
                      strSubDir = strSubDir & strPath & strReturn & ";"
                  End If
              End If
          End If
          strReturn = Dir()
      Loop
    '---------------------------------------------------------
    
      Dim intPos As Integer
      Do Until strSubDir = ""
          'PURPOSE: Locate the first ";" position - which represent the first directory
          intPos = InStr(1, strSubDir, ";")
          
          'PURPOSE: Strip the first directory name
          strReturn = Left$(strSubDir, intPos - 1)
          frmMain_Directory.List1.AddItem strReturn
          
          'PURPOSE: Recursive - go into the 1st level of each sub directory
          Call p_Get_SubDirectories(strReturn)
          
          'PURPOSE: Strip out the first directory name since
          '         the file doesn't exsit in that directory
          strSubDir = Mid(strSubDir, intPos + 1)  'Strip what you don't need
      Loop
    End Sub
    Chemically Formulated As:
    Dr. Nitro

  3. #3
    Guest
    use a directory box in VB.

  4. #4

    Thread Starter
    Hyperactive Member rockies1's Avatar
    Join Date
    Jul 1999
    Location
    Stuck at work
    Posts
    375
    The meat of the question is this: Does it have to be a DirListBox?
    Morgan
    [email protected] - Home
    [email protected] - Work
    Using VB6 SP6 but trying to learn VB2005EE

  5. #5
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    Use my codes with a regular listbox.
    Chemically Formulated As:
    Dr. Nitro

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width