Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Visual Basic directory search

Posted on 1998-05-27
8
Medium Priority
?
577 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
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 200 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Suggested Courses

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