Results 1 to 3 of 3

Thread: EXCEL: FileSearch seems to prefix filename with "*" YUK! [Resolved]

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Resolved EXCEL: FileSearch seems to prefix filename with "*" YUK! [Resolved]

    Esteemed Forum Participants and Lurkers:
    ===============================
    EXCEL

    I'm trying to identify a list of numbered files from a folder. They all have exactly the same format: fname_000.ext where "000" is any 3 numerals. I'm using the VBA FileSearch object, but it seems to prefix the filename with a wildcard "*" so that not only do all the files with the correct name get returned, but ANY file with any prefix before the correct name gets returned, such as "XXXX-fname_123.xls" ... YUK! How do I get it to ignore all files that do NOT fit the prototype?
    Code:
        'Create a FileSearch object
        Set fs = Application.FileSearch
        
        With fs
            .NewSearch
            .LookIn = "C:\Test"
            .SearchSubFolders = False
            .Filename = "fname_???.xls"             '<<< This is what fails
            .MatchTextExactly = True
            
            If .Execute > 0 Then
    
                'Step through each string in the FileDialogSelectedItems collection.
                For Each vrtSelectedItem In .FoundFiles
    
                    'vrtSelectedItem is a String: contains path of each selected item.
                    'This example simply displays the path in a message box.
                    'TEST TEST TEST TEST
                    MsgBox "The path is: " & vrtSelectedItem
                    'END TEST
                     
                Next vrtSelectedItem
            'The user pressed Cancel.
            Else
                'No file was selected
            End If
        
        End With
    Why the devil does this return a file named "XXXX-fname_123.xls" ??? YUK!

    And how do I only return files that EXACTLY fit my template: fname_???.xls
    Thank you for all comments, suggestions, and assistance.
    Last edited by Webtest; Jun 3rd, 2005 at 07:00 AM. Reason: RESOLVED
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  2. #2
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: EXCEL: FileSearch seems to prefix filename with "*" YUK!

    Well, you can correct for this error like so:

    VB Code:
    1. 'Create a FileSearch object
    2.     Set fs = Application.FileSearch
    3.    
    4.     With fs
    5.         .NewSearch
    6.         .LookIn = "C:\Test"
    7.         .SearchSubFolders = False
    8.         .Filename = "fname_???.xls"             '<<< This is what fails
    9.         .MatchTextExactly = True
    10.        
    11.         If .Execute > 0 Then
    12.  
    13.             'Step through each string in the FileDialogSelectedItems collection.
    14.             For Each vrtSelectedItem In .FoundFiles
    15.  
    16.                 'vrtSelectedItem is a String: contains path of each selected item.
    17.                 'This example simply displays the path in a message box.
    18.                 'TEST TEST TEST TEST
    19.                 MsgBox "The path is: " & Right$(vrtSelectedItem, Len(vrtSelectedItem) - InStr(vrtSelectedItem, "-"))
    20.                 'END TEST
    21.             Next vrtSelectedItem
    22.         'The user pressed Cancel.
    23.         Else
    24.             'No file was selected
    25.         End If
    26.     End With

    That should fix it but as to the cause I have no idea.

    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: EXCEL: FileSearch seems to prefix filename with "*" YUK!

    Thanks Ryan ...

    Yes, that's one way to do it, but it certainly is brain damaged. It is odd to note that the FileSearch works 'as expected' in Office '97! My default is Office 2002, and that is what is broken.

    I think I'll just use a Dir loop. Immediately previous to the FileSearch operation, I use the FileDialog, and except for extension filtering (part of the time I'm looking for PDFs which aren't recognized by MegaShaft), it works OK and returns the full file specification. The file spec and wild cards work perfectly in Dir.

    Thanks again ...
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

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