Speed Searching through Hard drive : vb6

The  code below searches through the c:\ drive and when it finds filenames contained
in "C:\input.txt" it add them to a listbox.

problem is if i have to many filenames to search for or the "C:\input.txt" file is
large then the program crawls

i need to speed this code 1000% , its just too slow.

' command button , label , listbox

Dim fso As New FileSystemObject   ' Add microsoft scripting runtime reference
Dim fld As Folder
Const fsoForReading = 1

Private Sub Command1_Click()
    Dim nDirs As Long, nFiles As Long, lSize As Currency
    Dim sDir As String, sSrchString() As String
    Dim strInputFileName As String
    Dim objTextStream As TextStream

    Dim i As Integer
    sDir = "c:\"
    strInputFileName = "C:\input.txt"
    Set objTextStream = fso.OpenTextFile(strInputFileName, fsoForReading)

    ReDim Preserve sSrchString(i)
    Do While Not objTextStream.AtEndOfStream
        i = UBound(sSrchString) + 1
        ReDim Preserve sSrchString(i)
        sSrchString(i) = objTextStream.ReadLine

    Label1.Caption = "Searching " & vbCrLf & UCase(sDir) & "..."
    lSize = FindFile(sDir, sSrchString, nDirs, nFiles)
    MsgBox Str(nFiles) & " files found in" & Str(nDirs) & " directories", vbInformation
    MsgBox "Total Size = " & lSize & " bytes"

End Sub

Private Function FindFile(ByVal sFol As String, sFile() As String, nDirs As Long, nFiles As Long) As Currency
   Dim tFld As Folder, tFil As File, FileName As String
   On Error GoTo Catch
   Set fld = fso.GetFolder(sFol)
   For i = 1 To UBound(sFile)
        FileName = Dir(fso.BuildPath(fld.Path, sFile(i)), vbNormal Or vbHidden Or vbSystem Or vbReadOnly)
        While Len(FileName) <> 0
           FindFile = FindFile + FileLen(fso.BuildPath(fld.Path, FileName))
           nFiles = nFiles + 1
           List1.AddItem fso.BuildPath(fld.Path, FileName)  ' Load ListBox
           FileName = Dir()  ' Get next file
   Next i
   Label1 = "Searching " & vbCrLf & fld.Path & "..."
   nDirs = nDirs + 1
   If fld.SubFolders.Count > 0 Then
      For Each tFld In fld.SubFolders
         FindFile = FindFile + FindFile(tFld.Path, sFile, nDirs, nFiles)
   End If
   Exit Function
Catch:  FileName = ""
       Resume Next
End Function
Who is Participating?
VBtormentConnect With a Mentor Commented:
Yo Jimmy
Here is a faster one, using api
and an aray for the info.txt
hope this work for ya :)

Private Const MAX_PATH = 260
Private Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
    dwFileAttributes As Long
    ftCreationTime As FILETIME
    ftLastAccessTime As FILETIME
    ftLastWriteTime As FILETIME
    nFileSizeHigh As Long
    nFileSizeLow As Long
    dwReserved0 As Long
    dwReserved1 As Long
    cFileName As String * MAX_PATH
    cAlternate As String * 14
End Type

Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long

Dim aFiles(0 To 100) As String 'number of files in your info.txt (use redim)
Dim iCount%

Private Sub ReadInfoTXT()
Dim sRow$
    'put all files in info.txt in a collection
    Open "C:\Info.txt" For Input As #1
        Do While Not EOF(1)
            Line Input #1, sRow$
            aFiles(iCount%) = sRow$
            iCount% = iCount% + 1
    Close #1
End Sub

Private Function TrimNull(startstr As String) As String
Dim pos As Integer

    pos = InStr(startstr, Chr$(0))
    If pos Then
        TrimNull = Left$(startstr, pos - 1)
        Exit Function
    End If
     TrimNull = startstr
End Function

Private Function bsl(Path$) As String
'Always return backslash at end of string
    If Right$(Path$, 1) = "\" Then
        bsl = Path$
        bsl = Path$ & "\"
    End If
End Function

Sub CountFolders(sPath$)
Dim i%
Dim hFile As Long
Dim cont As Integer
Dim sFile As String
    cont = True
    sPath$ = bsl(sPath$)
    hFile = FindFirstFile(sPath & "*.*" & Chr$(0), WFD)
    If hFile <> -1 Then
        While cont
            sFile = TrimNull(WFD.cFileName)
            If (sFile <> ".") And (sFile <> "..") Then
                If (WFD.dwFileAttributes And vbDirectory) Then 'Folders
                    CountFolders sPath$ & sFile 'recurse
                Else    'files
                    For i% = 0 To iCount%
                        If UCase(aFiles(i%)) = UCase(sFile) Then
                            'Add to listbox here
                            List1.AddItem sPath & sFile
                        End If
                End If
            End If
            cont = FindNextFile(hFile, WFD)
    End If
    Call FindClose(hFile)
End Sub

Private Sub Form_Load()
    iCount% = 0
    CountFolders "C:\"
End Sub
Mike TomlinsonMiddle School Assistant TeacherCommented:
You could try modifying the API FindFirstFile example from AllAPI:

The example is at the bottom of the page:

Jimmyx1000Author Commented:
VBtorment  ,

Hello Expert

VBtorment  ,can you tell me please

Where i can i add a label command so
i can actually see what is happening as the files are being searched.

VBtorment  ,thankyou

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Jimmyx1000Author Commented:
Vbtorment Good Work Well Done

I like the speed in your code , Good work

Vbtorment for an extra 500 points can you code me the pause and stop buttons

Thanks Expert.

Yo Jimmy

First for the label
 you can add it in the CountFolders sub (see below)

Sub CountFolders(sPath$)

               Else    'files
                   'Add Label here
                   Label1.caption=sPath$ & sFile
                   Doevents 'this is a must or else ya won't see a thing, but it will flikker the screen because this is fast!!
                    For i% = 0 To iCount%
End sub
Hai Jimmy

the pause button is a tricky one
i don't want to use it, but i don't know any other way...

use a boolean bWait (true/false) in a command button (bwait=not bwait)

At the top of the CountFolder sub add these lines

while bWait

this will hold the application as long as bWait = true
Remeber this is a tricky one (watch yor proccesor in the tasklist)
Maybe better ask another Expert for this :)
Jimmyx1000Author Commented:
thanks vbtorment

Good Work , Let me know how i can transfer more points to you .


but i don't know how you can do that, i'm new here :)
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.