How to search a disk for files?

Posted on 1998-06-10
Medium Priority
Last Modified: 2010-05-03
Can someone provide me with some code to use searching thrugh a disk for files?
Question by:Olli083097
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

swilt earned 200 total points
ID: 1462989
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$
    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$
    For Each vIte In colDir
        MyFindFiles vIte, sFilePattern
    Next vIte
    Set colDir = Nothing
End Sub


Expert Comment

ID: 1462990
This is a routine searching a directory for all the files.
If you have questions, let me know

    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,
               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.

    End sub


Expert Comment

ID: 1462991
There is a sample project for searching the disk for files. It is loacted under your VB directory in \samples\filectls (winseek.vbp).

Author Comment

ID: 1462992
Thank you all for trying to help!
swilt: Your code was just what I needed.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question