Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1626
  • Last Modified:

How do I use Filesearch with VB6?

I haven't been able to find a working sample of filesearch to find a file anywhere on a system, to include network drives, if they exist.
Anyone have code that I can use?
Thanks,
Russ
0
RUSS_EMI
Asked:
RUSS_EMI
  • 6
  • 4
  • 3
  • +1
2 Solutions
 
ZenMasterrrCommented:
0
 
GrahamSkanRetiredCommented:
This is what I use

Option Explicit

Public Function DriveExists(TargetDrive As String)
    On Error GoTo DriveExistsError
    ChDrive TargetDrive
    DriveExists = True
DriveExistsExit:
    Exit Function
DriveExistsError:
    Resume DriveExistsExit
End Function


Sub LookForFiles(strFolder As String, strFiles() As String, strFilePattern As String, iFileCount As Integer)
Dim strFileName
Dim strFolders() As String
Dim iFolderCount As Integer
Dim strFilePatterns
Dim i As Integer

strFileName = Dir$(strFolder & "\*", vbDirectory)
Do Until strFileName = ""
    If (GetAttr(strFolder & "\" & strFileName) And vbDirectory) = vbDirectory Then
        If Left$(strFileName, 1) <> "." Then
            ReDim Preserve strFolders(iFolderCount)
            strFolders(iFolderCount) = strFolder & "\" & strFileName
            iFolderCount = iFolderCount + 1
        End If
    End If
    strFileName = Dir$()
Loop

strFilePatterns = Split(strFilePattern, ";")
For i = 0 To UBound(strFilePatterns)
    strFileName = Dir$(strFolder & "\" & strFilePatterns(i), vbDirectory)
    Do Until strFileName = ""
        ReDim Preserve strFiles(iFileCount)
        strFiles(iFileCount) = strFolder & "\" & strFileName
        iFileCount = iFileCount + 1
        strFileName = Dir$()
    Loop
Next i
For i = 0 To iFolderCount - 1
    LookForFiles strFolders(i), strFiles(), strFilePattern, iFileCount
Next i
End Sub

Private Sub Command1_Click()
Dim strFiles() As String
Dim iFileCount As Integer
Dim iDriveNumber As Integer
Dim strDriveletter As String
For iDriveNumber = 1 To 26
    strDriveletter = Chr$(Asc("A") + iDriveNumber - 1)
    If DriveExists(strDriveletter) Then
        LookForFiles strDriveletter & ":", strFiles(), "*.frm;*.bas", iFileCount
    End If
Next iDriveNumber
End Sub


0
 
RUSS_EMIAuthor Commented:
Thanks to both of you.  
The first example finds them OK, but I must specify the drive and only the extension.
The second I'm not clear on where I will see the results (I did add command1 on the form)
Here is what I need to do;
1. find all occurrences of a constant FILENAME.EXT.
2. If there is only 1 found display it in a msgbox, if more then 1, display all locations in a combobox for the end user to select one.
Please excuse me for not thinking this all the way through the first time.
Russ
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
ZenMasterrrCommented:
russ .... go with grahamskan's code to search and locate these files, and use the code from the example i indicated to add to list or combo

zen :))
0
 
RUSS_EMIAuthor Commented:
zen,
   Thanks.  I tried to run the code, (after adding a command1), but got an error 32 (bad filename or number).  Am I missing a reference or forgot to initialize a variable?
Russ
0
 
ZenMasterrrCommented:
command1 is the get button I asssume ?
0
 
ajaikumarrCommented:
Hai,

I've just changed [GrahamSkan] code to get it run.
Add a Combo1, Text1, Command1 to run this code.

Option Explicit

Public Function DriveExists(TargetDrive As String)
    On Error GoTo DriveExistsError
    ChDrive TargetDrive
    DriveExists = True

DriveExistsExit:
    Exit Function

DriveExistsError:
    Resume DriveExistsExit
End Function


Sub LookForFiles(strFolder As String, strFiles() As String, strFilePattern As String, iFileCount As Integer)
    On Error GoTo LookForFilesErrorHandler
    Dim strFileName
    Dim strFolders() As String
    Dim iFolderCount As Integer
    Dim strFilePatterns
    Dim i As Integer
   
    strFileName = Dir$(strFolder & "\*", vbDirectory)
    Do Until strFileName = ""
        If (GetAttr(strFolder & "\" & strFileName) And vbDirectory) = vbDirectory Then
            If Left$(strFileName, 1) <> "." Then
                ReDim Preserve strFolders(iFolderCount)
                strFolders(iFolderCount) = strFolder & "\" & strFileName
                iFolderCount = iFolderCount + 1
            End If
        End If
        strFileName = Dir$()
    Loop
   
    strFilePatterns = Split(strFilePattern, ";")
    For i = 0 To UBound(strFilePatterns)
        strFileName = Dir$(strFolder & "\" & strFilePatterns(i), vbDirectory)
        Do Until strFileName = ""
            ReDim Preserve strFiles(iFileCount)
            strFiles(iFileCount) = strFolder & "\" & strFileName
            Me.Combo1.AddItem (strFiles(iFileCount))
            iFileCount = iFileCount + 1
            strFileName = Dir$()
        Loop
    Next i
    For i = 0 To iFolderCount - 1
        LookForFiles strFolders(i), strFiles(), strFilePattern, iFileCount
    Next i

LookForFilesErrorHandler:
    On Error GoTo 0
    Exit Sub
End Sub

Private Sub Command1_Click()
    If Me.Text1.Text = "" Then
        MsgBox ("Please provide the file name")
        Exit Sub
    End If
   
    Dim strFiles() As String
    Dim iFileCount As Integer
    Dim iDriveNumber As Integer
    Dim strDriveletter As String
    For iDriveNumber = 1 To 26
        strDriveletter = Chr$(Asc("A") + iDriveNumber - 1)
        If DriveExists(strDriveletter) Then
            LookForFiles strDriveletter & ":", strFiles(), Me.Text1.Text, iFileCount
        End If
    Next iDriveNumber
End Sub

Bye
Ajai
0
 
RUSS_EMIAuthor Commented:
With no explanation either, I hooked G's code for Command1_Click() to command1 button.  The program errors when the strFileName = "??? ???????? ????????.doc" in a large directory.  This while I am actually searching for "*.mdb" (of which there are only 2 dozen or so).  Can you get the above code to work?
Russ
0
 
ajaikumarrCommented:
Hai,

Here you can see a bunch of file search with Explanations.
http://vbnet.mvps.org/index.html?code/toc/tocbytopic_b.htm

Bye
Ajai
0
 
ajaikumarrCommented:
Hai,

Please run the code which i posted... It's working fine here...

Bye
Ajai
0
 
GrahamSkanRetiredCommented:
Hi RUSS_EMI,

I'm sorry I didn't provide the topping and tailing. I wasn't sure of your context. I presumed that you could do that yourself.

The array: 'strFiles' in this sub should hold the results

Private Sub Command1_Click()
Dim strFiles() As String
Dim iFileCount As Integer
Dim iDriveNumber As Integer
Dim strDriveletter As String
For iDriveNumber = 1 To 26
    strDriveletter = Chr$(Asc("A") + iDriveNumber - 1)
    If DriveExists(strDriveletter) Then
        LookForFiles strDriveletter & ":", strFiles(), "*.frm;*.bas", iFileCount
    End If
Next iDriveNumber
End Sub

Regarding the error that you report. What is it?
0
 
RUSS_EMIAuthor Commented:
I figured the strFiles out, the problem is I get an error 32 gad filename or number when I run it on my PC.  I don't know why, perhaps it is a number limitation?  Running the debugger the filename at the error is "??? ??????? ??? ??????.doc".  even though i'm looking for "*.mdb".
Thanks,
Russ
0
 
GrahamSkanRetiredCommented:
You don't say where in the program that you get the error, but I would guess it's in the folder search part. Is it on the GetAttr line?
The pattern to look for folders does not exclude any files - folder names can have an extension. If it finds one with an illegal file name, then it will give an error.

I've interpreted '32 gad filename or number' as '52 Bad file name or number' and added an error routine to skip to the next file or folder if that happens. The procedure will terminate if any other error occurs.

I can't test it because I can't create or rename a file to match yours. I would suggest that you delete the file.

Sub LookForFiles(strFolder As String, strFiles() As String, strFilePattern As String, iFileCount As Integer)
Dim strFileName
Dim strFolders() As String
Dim iFolderCount As Integer
Dim strFilePatterns
Dim i As Integer
Dim bGoodName As Boolean
On Error GoTo LookForFilesError
strFileName = Dir$(strFolder & "\*", vbDirectory)
Do Until strFileName = ""
    bGoodName = True
    If (GetAttr(strFolder & "\" & strFileName) And vbDirectory) = vbDirectory Then
        If bGoodName Then
            If Left$(strFileName, 1) <> "." Then
                ReDim Preserve strFolders(iFolderCount)
                strFolders(iFolderCount) = strFolder & "\" & strFileName
                iFolderCount = iFolderCount + 1
            End If
        End If
    End If
    strFileName = Dir$()
Loop

strFilePatterns = Split(strFilePattern, ";")
For i = 0 To UBound(strFilePatterns)
    strFileName = Dir$(strFolder & "\" & strFilePatterns(i), vbDirectory)
    Do Until strFileName = ""
        ReDim Preserve strFiles(iFileCount)
        strFiles(iFileCount) = strFolder & "\" & strFileName
        iFileCount = iFileCount + 1
        strFileName = Dir$()
    Loop
Next i
For i = 0 To iFolderCount - 1
    LookForFiles strFolders(i), strFiles(), strFilePattern, iFileCount
Next i
LookForFilesExit:
Exit Sub
LookForFilesError:
Select Case Err.Number
    Case 52
        bGoodName = False
        Resume Next
    Case Else
        MsgBox "Unexpected error: " & Err.Number & ", " & Err.Description
        Resume LookForFilesExit
End Select
End Sub


0
 
RUSS_EMIAuthor Commented:
Thank you, you probably are correct on the error number and problem area.  I will try this code and get back to you.
Again, thank you for your time and efforts.
Russ
0
 
RUSS_EMIAuthor Commented:
Thank you all.  I gave most points G., because it was his code that I used primarily.  I gave the accepted solution to A. because it was most complete, functional answer for future reference.  I trust that is satisfactory.
0
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.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

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

  • 6
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now