Solved

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

Posted on 2004-09-26
3
264 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DO Loop not working 4 72
Need VBS version of Excel date conversion 4 65
How to Add / Edit Windows Menu 4 52
How to make an ADE file by code? 11 81
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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

863 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

21 Experts available now in Live!

Get 1:1 Help Now