filename filtering source code wanted

Posted on 1997-11-26
Last Modified: 2010-04-04

I am looking for source code for a filter for filenames, using wildcards (* and ?) in the same way as the DOS command "dir" (dir *.exe, dir *a?b.e*, ...).
I tried to make it myself, but it is a hard stuff to be really general (try with filter "*y?z*" and fileName "aayynz".)

Where can I find such an algorithm or directly source code?
Question by:poupou
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

JimBob091197 earned 50 total points
ID: 1351932

Try the following function:

function MatchStrings(Source, Pattern: String): Boolean;
      pSource: Array [0..255] of Char;
      pPattern: Array [0..255] of Char;
      function MatchPattern(element, pattern: PChar): Boolean;
            function IsPatternWild(pattern: PChar): Boolean;
                  Result := StrScan(pattern,'*') <> nil;
                  if not Result then Result := StrScan(pattern,'?') <> nil;
    if 0 = StrComp(pattern,'*') then
      Result := True
    else if (element^ = Chr(0)) and (pattern^ <> Chr(0)) then
      Result := False
    else if element^ = Chr(0) then
      Result := True
            else begin
      case pattern^ of
      '*': if MatchPattern(element,@pattern[1]) then
             Result := True
             Result := MatchPattern(@element[1],pattern);
      '?': Result := MatchPattern(@element[1],@pattern[1]);
                        if element^ = pattern^ then
          Result := MatchPattern(@element[1],@pattern[1])
          Result := False;
      StrPCopy(pSource, source);
      StrPCopy(pPattern, pattern);
      Result := MatchPattern(pSource,pPattern);

Call: IsMatch := MatchStrings('HelloWorld.pas', '*.pas');


P.S.  I apologize in advance for the loss of code indentation...  I don't know why this happens.

Author Comment

ID: 1351933
wonderful algorithm!!

thanks a lot

Featured Post

Industry Leaders: 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

Suggested Solutions

Title # Comments Views Activity
code issue 8 188
IdTCPClient and IdTCPServer exchanging data on desktops and not Android 11 170
How to make Sign in, using Clientdataset? 1 33
Delphi, TImage, Png 2 32
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In an interesting question ( here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

730 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