Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 167
  • Last Modified:

Finding File & Folders

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!

Thanks!
0
Technokid
Asked:
Technokid
1 Solution
 
EgoreCommented:
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 FILE_ATTRIBUTE_DIRECTORY = &H10
Public Const FILE_ATTRIBUTE_NORMAL = &H80
Public Const INVALID_HANDLE_VALUE = -1
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
        Do
            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)
                Else
                    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
0
 
TechnokidAuthor Commented:
Thank You!!!!!!!!!!
0

Featured Post

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now