Solved

Visual Basic directory search

Posted on 1998-05-27
8
573 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Search combo error "Data Type Mismatch in Criteria Expression" 2 69
VB6 - Convert HH:MM into Decimal 8 62
RUNRMTCMD from AS/400 12 69
transition to visual .net from vb6 5 37
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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…

860 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