Finding File & Folders

Posted on 2003-03-12
Medium Priority
Last Modified: 2010-05-01
How can I tell VB to find files & folders that contain a user-defined string in them.  And then, put whatever it finds into a listbox?
I need an answer ASAP!

Question by:Technokid
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

Accepted Solution

Egore earned 240 total points
ID: 8122611
Here is some code that I use often (slightly modified to fit your needs, so hopefully it still works as intended):

' Directory list API calls
Public Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Public Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Public Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
Public Type FILETIME
        dwLowDateTime As Long
        dwHighDateTime As Long
End Type
Public Const MAX_PATH = 260
Public 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 * MAX_PATH
        cAlternate As String * 14
End Type
Public Const ERROR_NO_MORE_FILES = 18&

Public Sub GetFileList(Folder As String, Crit
As String, Lst As ListBox)
    Dim FileName As String
    Dim FileData As WIN32_FIND_DATA
    Dim FileHandle As Long, Ret As Long
    ' Get the first file
    FileHandle = FindFirstFile(Folder & Crit, FileData)
    If FileHandle <> INVALID_HANDLE_VALUE Then
        ' Loop through all of the files in the directory
            If (FileData.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) <> FILE_ATTRIBUTE_DIRECTORY Then
                ' Store the filename
                If InStr(1, FileData.cFileName, Chr(0)) Then
                    FileName = Left(FileData.cFileName, InStr(1, FileData.cFileName, Chr(0)) - 1)
                    FileName = Trim(FileData.cFileName)
                End If
                ' Add to our list
                Lst.AddItem FileName
            End If
            ' Get next file
            Ret = FindNextFile(FileHandle, FileData)
        Loop Until Ret = 0 Or Ret = ERROR_NO_MORE_FILES
        ' Free up our resources
        Call FindClose(FileHandle)
    End If

End Sub

Author Comment

ID: 8122960
Thank You!!!!!!!!!!

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
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…
Suggested Courses
Course of the Month10 days, 23 hours left to enroll

770 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