Results 1 to 6 of 6

Thread: Filtering unique items

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2000
    Location
    Houston, Texas - U.S.A.
    Posts
    84

    Question Filtering unique items

    I've got a FileListBox in which I'm displaying all the files in a directory. I'd like to allow the user to filter files based on the existing (displayed) file exentensions. I've succeeded in doing this but it's very slow on directories with a lot of files. Basically I've populated a dynamic array with all extensions then pull one out, cycle through looking for duplicates, then go to the next one. I then add the unique extensions to a Combo Box. I know there's got to be a better and faster way. Any help is much appreciated! Thanks!

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Check out the Pattern property of the FileListBox. Is that what you are talking about?
    VB Code:
    1. File1.Pattern = "*.exe"

  3. #3
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Well if you want to do duplicate checking there's nothing faster than the Dictionary object in the Microsoft Scripting Runtime reference.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    So what is the code you are using to get the unique file extensions?

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Here is how I'd do it:
    VB Code:
    1. Option Explicit
    2. 'declares to check combo
    3. Private Declare Function SendMessage Lib "User32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    4. Private Const CB_FINDSTRING = &H14C
    5. Private Const CB_FINDSTRINGEXACT = &H158
    6.  
    7. Private Sub FillCombo()
    8.     Dim fso As FileSystemObject
    9.     Set fso = New FileSystemObject
    10.    
    11.     Dim fldr As Folder
    12.     Dim fle As File
    13.    
    14.     'clear contents of combo
    15.     Combo1.Clear
    16.        
    17.     'get the folder
    18.     Set fldr = fso.GetFolder(File1.Path)
    19.     'loop thru files
    20.     For Each fle In fldr.Files
    21.        
    22.         Dim cbi As Integer
    23.         'test the extension against the combo
    24.         cbi = SendMessage(Combo1.hWnd, CB_FINDSTRINGEXACT, 0, ByVal fso.GetExtensionName(fle.Name))
    25.         If cbi = -1 Then
    26.             'it is a new extension so add it
    27.             Combo1.AddItem fso.GetExtensionName(fle.Name)
    28.         End If
    29.        
    30.     Next
    31.    
    32.     Set fldr = Nothing
    33.     Set fle = Nothing
    34.     Set fso = Nothing
    35. End Sub
    36.  
    37. Private Sub Combo1_Click()
    38.     'change pattern to selected
    39.     If Combo1.Text <> "" Then
    40.         File1.Pattern = "*." & Combo1.Text
    41.     End If
    42. End Sub
    43.  
    44. Private Sub File1_PathChange()
    45.     'path changed refill combo
    46.     FillCombo
    47. End Sub
    48.  
    49. Private Sub Form_Load()
    50.     'fill combo
    51.     FillCombo
    52. End Sub

    This assumes a reference to the Microsoft Scripting Runtime (for the FileSystemObject), a FileListBox (File1), a Combobox (Combo1)

  6. #6
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    I'll say it again, the Dictionary object is the fastest at keeping unique lists.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

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