Solved

Visual Basic directory search

Posted on 1998-05-27
8
576 Views
Last Modified: 2013-11-25
I am using Microsoft Visual Basic 4.0 (16 Bit) and am looking for a way to search through all directories on a drive as quickly as possible in order to locate files having a specific extension.  I have been able to do this using the Directory List Box and File List Box controls, but it runs much too slowly.  Is there a faster way to do this? (For example, Windows API commands?)
Good Luck  

Paul Murphy
0
Comment
Question by:jbarrett052798
[X]
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
8 Comments
 

Expert Comment

by:topol
ID: 1461829
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 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
   cAlternate As String * 14
End Type

Public Function FindFile(filename As String, Extn As string) As Variant

Dim FileNA() As String
Dim hSearch As Long
Dim wfd As WIN32_FIND_DATA
Dim r As Boolean
Dim Ex as string
Dim counter As Long
Dim filedone As Boolean
hSearch = FindFirstFile(filename, wfd)
If hSearch = INVALID_HANDLE_VALUE Then Exit Function
Do Until filedone = True
    Ex=ExtractExten(name)
    If Ex=Extn Then
        ReDim Preserve FileNA(counter)
        FileNA(counter) = TrimNull(wfd.cFileName)
        counter = counter + 1
    End If
    r = FindNextFile(hSearch, wfd)
    If r = False Then
        counter = GetLastError()
        If counter = ERROR_NO_MORE_FILES Then filedone = True
    End If
Loop
FindFile = FileNA
End Function

Private Function ExtractExten(name as string) as string
dim Extension as String
      If InStr(name, ".") Then
         For i = Len(name) To 1 Step -1
            If Mid(name, i, 1) = "." Then
               Extension = Mid(name, i + 1)
               Exit For
            End If
         Next i
      Else
         Extension = ""
      End If
ExtractExten=Extension
End function
'----------------------------------------------
This function uses 32-bit API, so you have to apply specific declarations instead of mine. I don't have 16-bit API--that's why i done it such way.
Hope it will help.
If any questions, please ask.

'----------------------------------------------
0
 

Expert Comment

by:topol
ID: 1461830
There is no 16-Bit equivalent declarations for these commands.  The ww31app1.txt file is 151kb and has 635 declarations, Why?

0
 

Author Comment

by:jbarrett052798
ID: 1461831
As far as I know, the only way to do this in 16 bit would be to write a C DLL to do the recursion with some C Functions.  I have created a DLL that does recursion but all it does is return the Total size of all files in the Directory and Sub Directories below it.

Have you ever done C?  Do you have a C Compiler?
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 7

Expert Comment

by:tward
ID: 1461832
I'm not sure why this cam back with no updated information?
0
 

Author Comment

by:jbarrett052798
ID: 1461833
jbarrett,
There is a very good sample project called WINSEEK.VBP that does exactly what you need, and is fast enough. It is located under your VB4 directory in the SAMPLES\FILECTLS sub-dir.
Regards
0
 
LVL 7

Expert Comment

by:kamall
ID: 1461834
Response to kamall: The winseek.vbp sample project is what I used as a basis for the method that I have already tried which runs too slowly.  Response to tward: I do not know C well enough to write a DLL and do not have a C compiler.
0
 

Author Comment

by:jbarrett052798
ID: 1461835
This will recursively search a drive for a file pattern in text1 text box i.e. "*.tmp" or a specific file, I hope it is fast enough

    Option Explicit

    Dim colFiles As Collection

    Private Sub Command1_Click()
        Dim vIte As Variant
         
        Set colFiles = New Collection
        MyFindFiles "C:\", Text1.Text
        For Each vIte In colFiles
            MsgBox vIte
        Next vIte
        Set colFiles = Nothing
         
    End Sub

    Private Sub MyFindFiles(ByVal sPath As String, ByVal sFilePattern As String)
        Dim sFile As String
        Dim vIte As Variant
        Dim colDir As New Collection
         
        On Error Resume Next
         
        sFile = Dir$(sPath & sFilePattern)
        While sFile <> ""
            If Left$(sFile, 1) <> "." Then
                colFiles.Add sPath & sFile
            End If
            sFile = Dir$
        Wend
             
        sFile = Dir$(sPath, vbDirectory)
        While sFile <> ""
            If Left$(sFile, 1) <> "." Then
                If GetAttr(sPath & sFile) And vbDirectory Then
                    colDir.Add sPath & sFile & "\"
                End If
            End If
            sFile = Dir$
        Wend
         
        For Each vIte In colDir
            MyFindFiles vIte, sFilePattern
        Next vIte
         
        Set colDir = Nothing
    End Sub
0
 
LVL 2

Accepted Solution

by:
swilt earned 100 total points
ID: 1461836
swilt.... "FAST ENOUGH"!!!!!
Your code requires at least double the time needed to find a file using the VB5 sample project WINSEEK.VBP!!!
Did you tried it?
I tested both of them.
Regards to all.
0

Featured Post

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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 Month8 days, 17 hours left to enroll

617 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