How to search a disk for files?

Can someone provide me with some code to use searching thrugh a disk for files?
Olli083097Asked:
Who is Participating?
 
swiltConnect With a Mentor Commented:
This will recursively search a drive for a file pattern in text1 text box i.e. "*.tmp" or a specific file

Option Explicit

Dim colFiles As Collection

Private Sub Command1_Click()
    Dim vIte As Variant
   
    Set colFiles = New Collection
    MyFindFiles "C:\", Text1.Text
    For Each vIte In colFiles
        MsgBox vIte
    Next vIte
    Set colFiles = Nothing
   
End Sub

Private Sub MyFindFiles(ByVal sPath As String, ByVal sFilePattern As String)
    Dim sFile As String
    Dim vIte As Variant
    Dim colDir As New Collection
   
    On Error Resume Next
   
    sFile = Dir$(sPath & sFilePattern)
    While sFile <> ""
        If Left$(sFile, 1) <> "." Then
            colFiles.Add sPath & sFile
        End If
        sFile = Dir$
    Wend
       
    sFile = Dir$(sPath, vbDirectory)
    While sFile <> ""
        If Left$(sFile, 1) <> "." Then
            If GetAttr(sPath & sFile) And vbDirectory Then
                colDir.Add sPath & sFile & "\"
            End If
        End If
        sFile = Dir$
    Wend
   
    For Each vIte In colDir
        MyFindFiles vIte, sFilePattern
    Next vIte
   
    Set colDir = Nothing
End Sub

0
 
DalinCommented:
Olli,
This is a routine searching a directory for all the files.
If you have questions, let me know
Regards
Dalin




    Private Sub cmdGetFileList_Click()

    Dim myFile As String
    Dim myPath As String
    Dim iposition As Integer

    myPath = "C:\YourDirectory\"

    myFile = Dir(myPath, vbDirectory)   ' Retrieve the first entry.
    Do While myFile <> ""   ' Start the loop.
        ' Ignore the current directory and
        If myFile <> "." And myFile <> ".." Then
             
            If (GetAttr(myPath & myFile) And vbDirectory) = vbDirectory Then
               ' a Sub Directory, Do nothing,
            Else
               iposition = InStr(1, myFile, ".")
               myFile = Left(myFile, iposition - 1)
               List1.AddItem myFile
                           
            End If  ' it represents a directory.
        End If
        myFile = Dir    ' Get next entry.
    Loop

    End sub



0
 
kamallCommented:
Olli,
There is a sample project for searching the disk for files. It is loacted under your VB directory in \samples\filectls (winseek.vbp).
0
 
Olli083097Author Commented:
Thank you all for trying to help!
swilt: Your code was just what I needed.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.