Solved

Visual Basic directory search

Posted on 1998-05-27
8
570 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
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
 
LVL 7

Expert Comment

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

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
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…

705 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

22 Experts available now in Live!

Get 1:1 Help Now