Solved

How to search a disk for files?

Posted on 1998-06-10
4
264 Views
Last Modified: 2010-05-03
Can someone provide me with some code to use searching thrugh a disk for files?
0
Comment
Question by:Olli083097
4 Comments
 
LVL 2

Accepted Solution

by:
swilt earned 50 total points
Comment Utility
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
 
LVL 9

Expert Comment

by:Dalin
Comment Utility
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
 
LVL 7

Expert Comment

by:kamall
Comment Utility
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
 

Author Comment

by:Olli083097
Comment Utility
Thank you all for trying to help!
swilt: Your code was just what I needed.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now