Results 1 to 2 of 2

Thread: [Class]class module file and folder search

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    7

    Resolved [Class]class module file and folder search

    After a while I return again to schedule a bit, and I set out to make a class module to search for files and folders.

    Some features include:

    Work with windows APIs, that gives us the opportunity to work with the files low.
    Ability to search hidden files.
    Possibility to search system folders.
    Ability to search hidden folders.
    Ability to Search Sub-Folders.
    The search is performed using regular expressions.
    Ability to search more than one Disk Drive.
    Easy to extend the class ... XD
    Functions For the pointer of the file icons to integrate for example in a Listview = D


    ClsBucarFiles.vb

  2. #2

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    7

    Re: [Class]class module file and folder search

    I leave the code for review and also contribute brothers.


    "vb.net Code:
    1. ''''''''''''''''''''''''''''''''''''''''''''''''''''''
    2. 'Agredecimiento a La web de Leandroascierto
    3. 'por darme las bases de como empezar
    4. '
    5. 'katze :=)
    6. 'fecha 22/12/2011 8:52 am v0.5
    7. ''''''''''''''''''''''''''''''''''''''''''''''''''''''
    8.  
    9. Option Strict Off
    10. Imports System.Runtime.InteropServices
    11. Imports System.Text.RegularExpressions
    12. Imports System.Text
    13. Imports System.IO
    14.  
    15. Public Class ClsBucarFiles
    16.  
    17. #Region "   Icon    "
    18.  
    19.     Private Structure SHFILEINFO
    20.         Public hIcon As IntPtr ' : icon
    21.         Public iIcon As Integer ' : icondex
    22.         Public dwAttributes As Integer ' : SFGAO_ flags
    23.         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _
    24.         Public szDisplayName As String
    25.         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _
    26.         Public szTypeName As String
    27.     End Structure
    28.  
    29.     Private Declare Ansi Function SHGetFileInfo Lib "shell32.dll" (ByVal pszPath As String, _
    30.     ByVal dwFileAttributes As Integer, ByRef psfi As SHFILEINFO, ByVal cbFileInfo As Integer, _
    31.     ByVal uFlags As Integer) As IntPtr
    32.  
    33.     Private Const SHGFI_SMALLICON As Long = &H1
    34.     Private Const SHGFI_SYSICONINDEX As Long = &H4000
    35.     Private Const SHGFI_USEFILEATTRIBUTES As Long = &H10
    36.     Private Const SHGFI_TYPENAME As Long = &H400
    37.     Private Const SHGFI_DISPLAYNAME As Long = &H200
    38.     Private Const SHGFI_ICON = &H100
    39.     Private Const SHGFI_LARGEICON = &H0         ' Large icon
    40.     Private Shared shfitmp As SHFILEINFO   'just used for the following
    41.     Private Shared SHFILESIZE As Integer = Marshal.SizeOf(shfitmp.GetType())
    42.  
    43.     Public Function ptricon(ByVal vsPath As String) As IntPtr
    44.         Dim hImgSmall As IntPtr
    45.         Dim shinfo As New SHFILEINFO()
    46.         shinfo.szDisplayName = New String(Chr(0), 260)
    47.         shinfo.szTypeName = New String(Chr(0), 80)
    48.         hImgSmall = SHGetFileInfo(vsPath, 0&, shinfo, SHFILESIZE, _
    49.         SHGFI_ICON Or SHGFI_SYSICONINDEX Or SHGFI_SMALLICON)
    50.         Return (shinfo.hIcon)
    51.     End Function
    52.  
    53. #End Region
    54.  
    55. #Region " Declaraciones "
    56.     <DllImport("kernel32.dll")> _
    57.     Private Shared Function FindClose(ByVal hFindFile As IntPtr) As Boolean
    58.     End Function
    59.  
    60.     <DllImport("kernel32.dll", CharSet:=CharSet.None)> _
    61.     Private Shared Function FindFirstFile(ByVal lpFileName As String, ByRef lpFindFileData As WIN32_FIND_DATA) As IntPtr
    62.     End Function
    63.  
    64.     Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As IntPtr, ByRef lpFindFileData As WIN32_FIND_DATA) As Boolean
    65.  
    66.     <StructLayout(LayoutKind.Sequential)> _
    67.     Structure WIN32_FIND_DATA
    68.         Public dwFileAttributes As UInteger
    69.         Public ftCreationTime As System.Runtime.InteropServices.ComTypes.FILETIME
    70.         Public ftLastAccessTime As System.Runtime.InteropServices.ComTypes.FILETIME
    71.         Public ftLastWriteTime As System.Runtime.InteropServices.ComTypes.FILETIME
    72.         Public nFileSizeHigh As UInteger
    73.         Public nFileSizeLow As UInteger
    74.         Public dwReserved0 As UInteger
    75.         Public dwReserved1 As UInteger
    76.         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> Public cFileName As String
    77.         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=14)> Public cAlternateFileName As String
    78.     End Structure
    79.  
    80.  
    81.     Public Event FileFound(ByVal sPath As String, ByVal sFile As String, ByVal atributos As FileAttributes)
    82.     Public Event FolderFound(ByVal sPath As String, ByVal sFolder As String, ByVal atributos As FileAttributes)
    83.  
    84.     Private cancel As Boolean = False
    85.  
    86.     Private INVALID_HANDLE_VALUE As New IntPtr(-1)
    87.  
    88.     Private RegExp As Regex
    89.  
    90.     Private sbFiles As New StringBuilder
    91.  
    92.     Private sbFolders As New StringBuilder
    93. #End Region
    94.  
    95. #Region " PRocedimientos "
    96.  
    97.     Public Sub New()
    98.  
    99.         MyBase.New()
    100.     End Sub
    101.  
    102.     Public Sub start()
    103.         If RegExp Is Nothing Then
    104.             RegExp = New Regex(p_match, RegexOptions.IgnoreCase)
    105.         Else
    106.             RegExp = Nothing
    107.             RegExp = New Regex(p_match, RegexOptions.IgnoreCase)
    108.         End If
    109.  
    110.         cancel = False
    111.  
    112.         Dim arrdrive() As String
    113.  
    114.         arrdrive = p_path.Split(Chr(59))
    115.  
    116.         For Each s As String In arrdrive
    117.             Call SubBusqueda(s) 'recursividad xD
    118.         Next
    119.        
    120.     End Sub
    121.  
    122.     Public Sub Cancelar()
    123.         cancel = True
    124.     End Sub
    125.     Private Function StripNulls(ByVal sData As String) As String
    126.         StripNulls = Left$(sData, Len(sData))
    127.     End Function
    128.     Private Sub SubBusqueda(ByVal vsPath As String)
    129.         Dim ptrSearch As New IntPtr
    130.         Dim vsfile As String = String.Empty
    131.         Dim vsfolder As String = String.Empty
    132.         Dim vaDir As New ArrayList
    133.         Dim wfd As New WIN32_FIND_DATA
    134.         Dim dir As String = String.Empty
    135.         Dim ret As Boolean
    136.  
    137.         If cancel Then Exit Sub
    138.  
    139.         Call NormalizePath(vsPath)
    140.         ptrSearch = FindFirstFile(vsPath & "*", wfd)
    141.  
    142.         If Not ptrSearch = INVALID_HANDLE_VALUE Then
    143.  
    144.             Do
    145.                 If (wfd.dwFileAttributes And FileAttributes.Directory) <> FileAttributes.Directory Then
    146.                     vsfile = StripNulls(wfd.cFileName)
    147.  
    148.                     If Not p_hide Then
    149.  
    150.                         If (wfd.dwFileAttributes And FileAttributes.Hidden) = FileAttributes.Hidden Then GoTo FNEXT
    151.  
    152.                     End If
    153.  
    154.                     If RegExp.Matches(vsfile).Count = 0 Then GoTo FNEXT
    155.  
    156.                     sbFiles.AppendLine(vsfile)
    157.  
    158.                     RaiseEvent FileFound(vsPath, vsfile, CType(wfd.dwFileAttributes, FileAttributes))
    159.                 Else
    160.  
    161.                     If Not p_hide Then
    162.                         If (wfd.dwFileAttributes And FileAttributes.Hidden) = FileAttributes.Hidden Then GoTo FNEXT
    163.                     End If
    164.  
    165.                     vsfolder = StripNulls(wfd.cFileName)
    166.  
    167.                     If (vsfolder <> ".") And (vsfolder <> "..") Then
    168.                         dir = vsPath & vsfolder & "\"
    169.  
    170.                         If Not p_sys Then
    171.                             Debug.Print(Environ("Windir"))
    172.                             If dir = Environ("Windir") & "\" Then GoTo FNEXT
    173.                         End If
    174.  
    175.                         vaDir.Add(dir)
    176.                         If RegExp.Matches(vsfolder).Count = 0 Then GoTo FNEXT
    177.                         sbFolders.AppendLine(vsfolder)
    178.                         RaiseEvent FolderFound(vsPath, vsfolder, CType(wfd.dwFileAttributes, FileAttributes))
    179.  
    180.                     End If
    181.                 End If
    182. FNEXT:
    183.  
    184.                 If cancel Then FindClose(ptrSearch) : Exit Sub
    185.                 Application.DoEvents()
    186.  
    187.                 ret = FindNextFile(ptrSearch, wfd)
    188.             Loop While ret
    189.             Call FindClose(ptrSearch)
    190.         End If
    191.  
    192.         If p_subfolder Then
    193.  
    194.             For i As Integer = 0 To vaDir.Count - 1
    195.  
    196.                 Call SubBusqueda(CStr(vaDir(i)))
    197.             Next
    198.  
    199.         End If
    200.     End Sub
    201.  
    202.     Private Function drivers() As String
    203.         Dim drive As DriveInfo
    204.         Dim dr As New StringBuilder
    205.  
    206.         For Each drive In DriveInfo.GetDrives
    207.             With drive
    208.                 If .IsReady Then
    209.                     dr.Append(drive.Name & ";")
    210.                 End If
    211.             End With
    212.         Next
    213.         Return dr.ToString
    214.     End Function
    215.  
    216.     Private Function ReplaceFilter(ByVal sFilter As String) As String
    217.         sFilter = sFilter.Replace("+", "\+")
    218.         sFilter = sFilter.Replace(".", "\.")
    219.         sFilter = sFilter.Replace("|", "\|")
    220.         sFilter = sFilter.Replace(";", "|\b")
    221.         sFilter = sFilter.Replace(" ", "|\b")
    222.         sFilter = sFilter.Replace("{", "\{")
    223.         sFilter = sFilter.Replace("}", "\}")
    224.         sFilter = sFilter.Replace("*", ".+")
    225.         sFilter = sFilter.Replace("?", ".{1}")
    226.         sFilter = sFilter.Replace("(", "\(")
    227.         sFilter = sFilter.Replace(")", "\)")
    228.         sFilter = sFilter.Replace("^", "\^")
    229.         sFilter = sFilter.Replace("$", "\$")
    230.         sFilter = sFilter.Replace("[", "\[")
    231.         sFilter = sFilter.Replace("[", "\]")
    232.  
    233.         Do While CBool(InStr(sFilter, "|\b|\b"))
    234.             sFilter = Replace$(sFilter, "|\b|\b", "|\b")
    235.         Loop
    236.         Return "^(" & sFilter & ")$|(" & sFilter & ".+)"
    237.     End Function
    238.  
    239.  
    240.     Public Function NormalizePath(ByRef sData As String) As String
    241.  
    242.         If Strings.Len(sData) > 1 Then
    243.             sData = Strings.Replace(sData, "/", "\")
    244.             If Not Strings.Right(sData, 1) = "\" Then
    245.                 Return sData & "\"
    246.             Else
    247.                 Return sData
    248.             End If
    249.         End If
    250.  
    251.     End Function
    252.  
    253.  
    254. #End Region
    255.  
    256. #Region " propiedades "
    257.     Private p_match As String = String.Empty
    258.  
    259.     WriteOnly Property Match() As String
    260.         Set(ByVal value As String)
    261.             p_match = ReplaceFilter(value)
    262.         End Set
    263.     End Property
    264.  
    265.     Private p_subfolder As Boolean = False
    266.     Property SubFolder() As Boolean
    267.         Get
    268.             Return p_subfolder
    269.         End Get
    270.         Set(ByVal value As Boolean)
    271.             p_subfolder = value
    272.         End Set
    273.     End Property
    274.     Private p_hide As Boolean = False
    275.     Property HideFolder() As Boolean
    276.         Get
    277.             Return p_hide
    278.         End Get
    279.         Set(ByVal value As Boolean)
    280.             p_hide = value
    281.         End Set
    282.     End Property
    283.     Private p_sys As Boolean = False
    284.     Property SysFolder() As Boolean
    285.         Get
    286.             Return p_sys
    287.         End Get
    288.         Set(ByVal value As Boolean)
    289.             p_sys = value
    290.         End Set
    291.     End Property
    292.     Private p_path As String = drivers()
    293.     Property Path() As String
    294.         Get
    295.             Return p_path
    296.         End Get
    297.         Set(ByVal value As String)
    298.             NormalizePath(value)
    299.             p_path = value
    300.         End Set
    301.     End Property
    302.     ReadOnly Property FilesString() As String
    303.         Get
    304.             Return sbFolders.ToString
    305.         End Get
    306.     End Property
    307.  
    308.     ReadOnly Property FolderString() As String
    309.         Get
    310.  
    311.             Return sbFolders.ToString
    312.         End Get
    313.     End Property
    314. #End Region
    315.  
    316. End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width