Link to home
Start Free TrialLog in
Avatar of jrcp
jrcpFlag for Afghanistan

asked on

FileListBpx vs. ?

Is there a way to loop through given folder without using a filelistbox or any other control that you must put on a form?

Thanks,
JP
Avatar of Ruchi
Ruchi

DriveListBox?
ASKER CERTIFIED SOLUTION
Avatar of mcrider
mcrider

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can use the FindFirstFile,FindNextfile API functions. The declarations are in the API text viewer app that ships with VB.


Good luck!!
Yes....

This program displays using a Folder object.

Option Explicit                    ' General declaration
Dim mFso As New FileSystemObject   ' General declaration

Private Sub dirBox_Click()
   Dim f As Folder
   
   ' Get a Folder to the selected item
   Set f = mFso.GetFolder(dirBox.List(dirBox.ListIndex))
   
   txtDisplay.Text = ""      ' Clear TextBox
   
   ' Test for the root folder
   If f.IsRootFolder = False Then
      txtDisplay.Text = "Root folder: " & f.IsRootFolder _
                        & vbNewLine & _
                        "Parent Folder: " & f.ParentFolder & _
                        vbNewLine & "Size: " & f.Size
   Else
      txtDisplay.Text = "Root folder: " & f.IsRootFolder
   End If
   
   txtDisplay.Text = txtDisplay.Text & vbNewLine & "Type: " & _
                     f.Type & vbNewLine & _
                     "Short Path: " & f.ShortPath & _
                     vbNewLine & "Path: " & f.Path & _
                     vbNewLine & "Short Name: " & f.ShortName
End Sub
This example is from the book.
Avatar of jrcp

ASKER

As for using the Dir Function then how do I know ahead of time how many files are in that dir?
You don't need to know ahead of time. You pass the arguements to it to retrieve the first match. Successive calls without parameters will return the next file in the folder matching the parameters. When there are no more matches, Dir returns "".

C:\Folder\
  cloud.bmp
  tree.bmp
  rock.bmp

Dir$("C:\Folder\*.bmp") 'Returns "C:\Folder\cloud.bmp"
Dir$() 'Returns "C:\Folder\tree.bmp"
Dir$() 'Returns "C:\Folder\rock.bmp"
Dir$() 'Returns ""

Hope this helps!
You don't know ahead of time...  You would loop through like this to get the number of files... Add the following code to a command button, run the program and watch the debug window:


Private Sub Command1_Click()
    Dim MyFile As String
    Dim iVal As Long
   
    MyFile = Dir("C:\WINDOWS\*.INI")
    Do While Not MyFile = ""
        iVal = iVal + 1
        Debug.Print "Found File #" + CStr(iVal) + ": " + MyFile
        MyFile = Dir
    Loop
    If iVal = 0 Then
        Debug.Print "There were not files found"
    Else
        Debug.Print "There were " + CStr(iVal) + " file found"
    End If
End Sub



Cheers!
Thanks for the points! Glad I could help!


Cheers!