Speed Searching through Hard drive : vb6

Posted on 2004-09-27
Medium Priority
Last Modified: 2010-05-02
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
Question by:Jimmyx1000
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
  • 4
  • 3
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12164853
You could try modifying the API FindFirstFile example from AllAPI:

The example is at the bottom of the page:


Accepted Solution

VBtorment earned 2000 total points
ID: 12164936
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

Author Comment

ID: 12166242
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Author Comment

ID: 12166567
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.


Expert Comment

ID: 12174589
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

Expert Comment

ID: 12174638
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 :)

Author Comment

ID: 12175144
thanks vbtorment

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


Expert Comment

ID: 12177976

but i don't know how you can do that, i'm new here :)

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month9 days, 12 hours left to enroll

762 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