Solved

filename filtering source code wanted

Posted on 1997-11-26
2
145 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
[X]
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
2 Comments
 
LVL 5

Accepted Solution

by:
JimBob091197 earned 50 total points
ID: 1351932
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
ID: 1351933
wonderful algorithm!!

thanks a lot
0

Featured Post

Technology Partners: 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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

707 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