Directory and file listing

Can someone please help me with some code.
I want to be able to put the contents of a drive or folder into 2 string variables, one with each filename and the other with the folders.
The values need to be separated by a ","
(no quotes) and preferably in alphabetical order.

Please supply some code...thanks.
And no. i do not want to use any controls to do this.
LVL 1
vbkannAsked:
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.

Erick37Commented:
Not in Alpha order:

Private Function GetDirs(sPath As String, FileOrDir As Integer) As String
    Dim sName As String
    Dim sFullList As String
    sPath = "c:\" 'Set the path.
    sName = Dir(sPath, FileOrDir) ' Get first directory
    Do While sName <> vbNullString  ' Start looping
        ' Ignore the current directory and the encompassing directory.
        If sName <> "." And sName <> ".." Then
            If (GetAttr(sPath & sName) And FileOrDir) = FileOrDir Then
                sFullList = sFullList & sName & "," 'Make the string
            End If
        End If
        sName = Dir 'Get next
    Loop
    sFullList = Left(sFullList, Len(sFullList) - 1) 'strip last ","
    GetDirs = sFullList
End Function

Private Sub Command1_Click()
    Text1.Text = GetDirs("c:\", vbDirectory) 'list direcories
    Text2.Text = GetDirs("c:\", vbNormal) 'list files
End Sub
0
VbmasterCommented:
Try this code..

  Dim Dirs() As String
  Dim Files() As String
  Dim a As Long
  Dim sFiles As String
  Dim sDirs As String
 
  Call clsFile.ListContent("E:\Mp3\", Dirs, Files, True)
 
  For a = 1 To UBound(Files)
    sFiles = sFiles & Files(a) & ","
  Next
  For a = 1 To UBound(Dirs)
    sDirs = sDirs & Dirs(a) & ","
  Next

... using this function..

Function ListContent(ByVal Path As String, ByRef Dirs() As String, ByRef Files() As String, Recursive As Boolean, Optional ByRef DirCount As Long, Optional ByRef Filecount As Long, Optional StartDir As Boolean = True, Optional IncludePaths As Boolean = True)

  Dim MyName  As String
  Dim MyDir() As String
  Dim MyDirNr As Integer
  Dim a       As Integer
 
  MyDirNr = 0
  ReDim MyDir(0)
  If StartDir Then
    ReDim Dirs(0)
    ReDim Files(0)
    Path = IIf(Right$(Path, 1) = "\", "", "\")
  End If
 
  On Error GoTo Errorhandler
 
  MyName = Dir$(Path + "*", vbDirectory + vbArchive + vbHidden + vbReadOnly + vbSystem)
 
  Do While (MyName <> "")
    If (MyName <> ".") And (MyName <> "..") Then
      If ((GetAttr(Path & MyName) And vbDirectory) <> vbDirectory) Then
        If (Filecount Mod 100 = 0) Then ReDim Preserve Files(Filecount + 100)
        Filecount = Filecount + 1
        If IncludePaths Then
          Files(Filecount) = Path + MyName
        Else
          Files(Filecount) = MyName
        End If
      Else
        If (DirCount Mod 100 = 0) Then ReDim Preserve Dirs(DirCount + 100)
        DirCount = DirCount + 1
        If IncludePaths Then
          Dirs(DirCount) = Path + MyName
        Else
          Dirs(DirCount) = MyName
        End If
       
        ReDim Preserve MyDir(UBound(MyDir) + 1)
        MyDirNr = MyDirNr + 1
        MyDir(MyDirNr) = MyName
      End If
    End If
    MyName = Dir
  Loop
 
  If Recursive Then
    For a = 1 To MyDirNr
      If Not ListContent(Path + MyDir(a) + "\", Dirs, Files, True, DirCount, Filecount, False, IncludePaths) Then GoTo Errorhandler
    Next
  End If
  If StartDir Then
    ReDim Preserve Files(Filecount)
    ReDim Preserve Dirs(DirCount)
  End If
  ListContent = True
  Exit Function
 
Errorhandler:
  ListContent = False

End Function
0
Erick37Commented:
Sorted:

Option Explicit

Private Function GetDirs(sPath As String, FileOrDir As Integer) As String
    Dim sName As String
    Dim temp As String
    Dim i As Integer
    Dim sFullList() As String
    ReDim sFullList(0)
    sPath = "c:\" 'Set the path.
    sName = Dir(sPath, FileOrDir) ' Get first directory
    Do While sName <> vbNullString  ' Start looping
        ' Ignore the current directory and the encompassing directory.
        If sName <> "." And sName <> ".." Then
            If (GetAttr(sPath & sName) And FileOrDir) = FileOrDir Then
                sFullList(i) = sName
                ReDim Preserve sFullList(UBound(sFullList) + 1)
                i = i + 1
            End If
        End If
        sName = Dir 'Get next
    Loop
    QuickSort sFullList, 0, UBound(sFullList)
    For i = 0 To UBound(sFullList)
        If Not sFullList(i) = vbNullString Then
            temp = temp & sFullList(i)
            If i < UBound(sFullList) Then
                temp = temp & ","
            End If
        End If
    Next
    GetDirs = temp
End Function

Private Sub Command1_Click()
    Text1.Text = GetDirs("c:\", vbDirectory) 'list direcories
    Text2.Text = GetDirs("c:\", vbNormal) 'list files
End Sub


' ============================ QuickSort ============================
' QuickSort works by picking a random "pivot" element in SortArray,
' then moving every element that is bigger to one side of the pivot,
' & every element that is smaller to the other side. QuickSort is
' then called recursively with the two subdivisions created by the
' pivot. Once the number of elements in a subdivision reaches two,
' the recursive calls end and the array is sorted.
' ===================================================================
'
Private Sub QuickSort(SortArray() As String, ByVal Low As Long, _
                   ByVal High As Long)
   Dim i As Long, J As Long, RandIndex As Long, Partition As String
   If Low < High Then
      ' Only two elements in this subdivision; swap them if they are
      ' out of order, then end recursive calls:
      If High - Low = 1 Then
         If UCase(SortArray(Low)) > UCase(SortArray(High)) Then
            SWAP SortArray(Low), SortArray(High)
         End If
      Else
         ' Pick a pivot element at random, then move it to the end:
         RandIndex = Rnd() * (High - Low) + Low ' RandInt%(Low, High)
         SWAP SortArray(High), SortArray(RandIndex)
         Partition = UCase(SortArray(High))
         Do
            ' Move in from both sides towards the pivot element:
            i = Low: J = High
            Do While (i < J) And (UCase(SortArray(i)) <= Partition)
               i = i + 1
            Loop
            Do While (J > i) And (UCase(SortArray(J)) >= Partition)
               J = J - 1
            Loop

            ' If we haven't reached the pivot element it means that 2
            ' elements on either side are out of order, so swap them:
            If i < J Then
               SWAP SortArray(i), SortArray(J)
            End If
         Loop While i < J

         ' Move the pivot element to its proper place in the array:
         SWAP SortArray(i), SortArray(High)

         ' Recursively call the QuickSort procedure (pass the
         ' smaller subdivision first to use less stack space):
         If (i - Low) < (High - i) Then
            QuickSort SortArray, Low, i - 1
            QuickSort SortArray, i + 1, High
         Else
            QuickSort SortArray, i + 1, High
            QuickSort SortArray, Low, i - 1
         End If
      End If
   End If
End Sub

Private Sub SWAP(first As String, second As String)
   Dim temp As String
   temp = first
   first = second
   second = temp
End Sub
0
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!

vbkannAuthor Commented:
eric37..this is perfect..if you could please modify it so that i can view all files...(hidden etc) and not just the normal files...


thanks
0
Erick37Commented:
Modified portion of code:

If sName <> "." And sName <> ".." Then
    If FileOrDir = vbDirectory Then
        If (GetAttr(sPath & sName) And FileOrDir) = FileOrDir Then
            sFullList(i) = sName
            ReDim Preserve sFullList(UBound(sFullList) + 1)
            i = i + 1
        End If
    Else
        sFullList(i) = sName
        ReDim Preserve sFullList(UBound(sFullList) + 1)
        i = i + 1
    End If
End If


Call the function like this to get all the files:

Text2.Text = GetDirs("c:\", vbNormal + vbSystem + vbHidden) 'list files
0

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
vbkannAuthor Commented:
thank you vey much
0
vbkannAuthor Commented:
wait a moment....how can i view the listings of other folders..and not just the root directory..

it doesnt work.

can you please modify the code to let me get listings of other folders....thanks
0
VbmasterCommented:
Try the code I posted, it works for recursive listing.
0
vbkannAuthor Commented:
I have tried your code and it is very slow.

Im sorry but i would rather use eric37's code as it is fast, if only it could do other folders. Can you please help me to be able to do this..thank you
0
Erick37Commented:
The code is fast because it only returns top level directories for any given path.  If you recurse into all subdirectories, any coding will slow down.
0
vbkannAuthor Commented:
But cant i just get a listing of all the directories and files in a path that i specify.
Just in that path.

I dont want to look into all subdirectories, i just want the files and directories in a given path, no subdirectories of those directories.

Can you explain to me how to do this....

cheers
0
vbkannAuthor Commented:
But cant i just get a listing of all the directories and files in a path that i specify.
Just in that path.

I dont want to look into all subdirectories, i just want the files and directories in a given path, no subdirectories of those directories.

Can you explain to me how to do this....

cheers
0
VbmasterCommented:
Gotta ask, you mean my code looked slow or did it take a longer time to process than erick's? <g>

I don't felt like optimizing the code, since I felt that someone that wanted to get all filenames/dirnames into one big string, can't be thinking about performance. ;)

0
Erick37Commented:
Yes there is a bug...

Get rid of the line
sPath = "c:\" 'Set the path.
in the function!

And if you want to view the contents of the windows directory call:

Private Sub Command1_Click()
    Text1.Text = GetDirs("c:\windows\", vbDirectory) 'list direcories
    Text2.Text = GetDirs("c:\windows\", vbNormal) 'list files
End Sub
0
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.