Solved

Adding  a stop button and pause button to file searching : vb6

Posted on 2004-09-26
3
267 Views
Last Modified: 2012-05-05
Id like to add a stop button and pause button to my code when a search is being processed.


Please Add

1 text box
1 command button

1 stop button    ' please add
1 paue button   ' please add


Private Declare Function SearchTreeForFile Lib "imagehlp" (ByVal RootPath As String, ByVal InputPathName As String, ByVal OutputPathBuffer As String) As Long
Private Const MAX_PATH = 260

Private Sub Command1_Click()
 
    Dim tempStr As String, Ret As Long
    'create a buffer string
    tempStr = String(MAX_PATH, 0)
    'returns 1 when successfull, 0 when failed
    Ret = SearchTreeForFile("c:\", Text1.Text, tempStr)
   
   
    If Ret <> 0 Then
       
       MsgBox "Located file at " + Left$(tempStr, InStr(1, tempStr, Chr$(0)) - 1)
   
    Else
        MsgBox "File not found!"
    End If

End Sub
0
Comment
Question by:Jimmyx1000
3 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12156643
Hi Jimmyx1000,

There is now way we can add this functionality with the code you have presented.

The line that actually does the search

     Ret = SearchTreeForFile("c:\", Text1.Text, tempStr)

appears to be a blocking call to an external function, meaning it doesn't return until it is finished.   To implement the Stop and Pause features, you would need to modify the SearchTreeForFile() function.

Regards,

Idle_Mind
0
 
LVL 1

Accepted Solution

by:
VBtorment earned 500 total points
ID: 12162894
Maybe this is helpfull
Not a nice solution but it works.

Option Explicit

Private bWait As Boolean
Private sWaitDir As String

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

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$
    Else
        bsl = Path$ & "\"
    End If
End Function

Sub CountFolders(sPath$)
Dim WFD As WIN32_FIND_DATA
Dim hFile As Long
Dim cont As Integer
Dim sFile As String

       
    While bWait 'wait for bwait to turn false again(not very nice but it works)
        DoEvents
    Wend
   
    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
                    Label1.Caption = sPath$ & sFile
                    DoEvents
                End If
            End If
            cont = FindNextFile(hFile, WFD)
        Wend
    End If
    Call FindClose(hFile)
End Sub


Private Sub cmdPause_Click()
    bWait = Not bWait
End Sub

Private Sub cmdStart_Click()
    bWait = False
    Call CountFolders("C:\")
End Sub

0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…

820 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