[Webinar] Learn how to a build a cloud-first strategyRegister Now

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

filename filtering source code wanted

hi,

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?
0
poupou
Asked:
poupou
1 Solution
 
JimBob091197Commented:
Hi

Try the following function:

function MatchStrings(Source, Pattern: String): Boolean;
var
      pSource: Array [0..255] of Char;
      pPattern: Array [0..255] of Char;
      function MatchPattern(element, pattern: PChar): Boolean;
            function IsPatternWild(pattern: PChar): Boolean;
            begin
                  Result := StrScan(pattern,'*') <> nil;
                  if not Result then Result := StrScan(pattern,'?') <> nil;
            end;
  begin
    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
           else
             Result := MatchPattern(@element[1],pattern);
      '?': Result := MatchPattern(@element[1],@pattern[1]);
      else
                        if element^ = pattern^ then
          Result := MatchPattern(@element[1],@pattern[1])
        else
          Result := False;
      end;
    end;
      end;
begin
      StrPCopy(pSource, source);
      StrPCopy(pPattern, pattern);
      Result := MatchPattern(pSource,pPattern);
end;


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

JB

P.S.  I apologize in advance for the loss of code indentation...  I don't know why this happens.
0
 
poupouAuthor Commented:
wonderful algorithm!!

thanks a lot
0

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!

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