Solved

filename filtering source code wanted

Posted on 1997-11-26
2
129 Views
Last Modified: 2010-04-04
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
Comment
Question by:poupou
2 Comments
 
LVL 5

Accepted Solution

by:
JimBob091197 earned 50 total points
Comment Utility
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
 

Author Comment

by:poupou
Comment Utility
wonderful algorithm!!

thanks a lot
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now