[fpc-pascal] findfirst wildcards...

waldo kitty wkitty42 at windstream.net
Wed Dec 3 17:30:12 CET 2014


On 12/3/2014 3:41 AM, Jonas Maebe wrote:
>
> On 03 Dec 2014, at 06:05, waldo kitty wrote:
>
>> can you help me understand the differences in the output of the below program?
>>
>> given these three filenames: t.txt t1.txt t11.txt
>>
>> with mask  t?.txt  why does MatchesMaskList not show t.txt and t11.txt like
>> plain findfirst?
>
> "?" means "a single arbitrary character".  findfirst with "t?.txt" should not
> match t.txt nor t11.txt either.

yes, i wrote t11.txt when i should have written t1.txt... my bad...

however on
   OS/2 native
   OS/2 with 4OS2 command interpreter replacement
   OS/2 DOS native
   OS/2 DOS with 4DOS command interpreter replacement
   Vista (32bit)
t?.txt returns both t.txt and t1.txt...


C:\freepascal\projects\misc>dir t?.txt

  Volume in drive C is SQ004512V04
  Volume Serial Number is DC02-9142

  Directory of C:\freepascal\projects\misc

12/02/2014  11:31 PM                 2 t.txt
12/02/2014  11:31 PM                 2 t1.txt
                2 File(s)              4 bytes
                0 Dir(s)  51,111,350,272 bytes free



C:\freepascal\projects\misc>filemask t?.txt

looking for files that match "t?.txt"

*FindFirst only - multiple masks capable*

   ff1(t?.txt);
     t.txt
     t1.txt


*FindFirst w/ MatchesMaskList*

   ff2(t?.txt);
     t1.txt


*FindFirst w/ RegExpr - multiple masks capable*

   ff3(t?.txt);
     t.txt
     t1.txt
     t11.txt



new filemask demo program follows...


===== snip filemask.lpr =====
Program Filemask;

Uses
   SysUtils, StrUtils, Classes, Masks, RegExpr;

var
   dirSR : TSearchRec;
   flist : TStringList;
   fmask : String;
   lcnt  : Integer;

procedure ff(tstr : String);
begin
   if FindFirst(tstr,faAnyFile,dirSR) = 0 then
     begin
       repeat
         flist.Add(dirSR.Name);
       until FindNext(dirSR) <> 0;
       {$IFDEF FPC}
       FindClose(dirSR);
       {$ENDIF}
     end;
end;

procedure ff1(tstr : String);
var
   tcnt  : Integer = 0;
   fmcnt : Integer = 0;
begin
   fmcnt := WordCount(tstr,[';']);
   if fmcnt = 0 then
     ff(tstr)
   else
     for tcnt := 1 to fmcnt do
       begin
         ff(ExtractWord(tcnt,tstr,[';']));
       end;
end;

procedure ff2(tstr : String);
begin
   if FindFirst('*',faAnyFile,dirSR) = 0 then
     begin
       repeat
         if MatchesMaskList(dirSR.Name,tstr) then
           flist.Add(dirSR.Name);
       until FindNext(dirSR) <> 0;
       {$IFDEF FPC}
       FindClose(dirSR);
       {$ENDIF}
     end;
end;

procedure ffregex(tstr : String);
var
   RegexObj: TRegExpr;
begin
   RegexObj := TRegExpr.Create;
   RegexObj.Expression := tstr;
   if FindFirst('*',faAnyFile,dirSR) = 0 then
     begin
       repeat
         if RegexObj.Exec(dirSR.Name) then
           flist.Add(dirSR.Name);
       until FindNext(dirSR) <> 0;
       {$IFDEF FPC}
       FindClose(dirSR);
       {$ENDIF}
     end;
   RegexObj.Free;
end;

procedure ff3(tstr : String);
var
   tcnt  : Integer = 0;
   fmcnt : Integer = 0;
begin
   fmcnt := WordCount(tstr,[';']);
   if fmcnt = 0 then
     ffregex(tstr)
   else
     for tcnt := 1 to fmcnt do
       begin
         ffregex(ExtractWord(tcnt,tstr,[';']));
       end;
end;


begin
   if ParamStr(1) = '' then
     WriteLn('please specify a file mask - eg: *.foo')
   else
     begin
       fmask := ParamStr(1);
       WriteLn;
       WriteLn('looking for files that match "'+fmask+'"');
       WriteLn;
       flist := TStringList.Create;
       flist.Sorted := True;
       flist.Duplicates := dupIgnore;
       flist.CaseSensitive := False;
       try
         WriteLn('*FindFirst only - multiple masks capable*');
         WriteLn;
         WriteLn('  ff1('+fmask+');');
         ff1(fmask);
         for lcnt := 1 to flist.Count do
           WriteLn('    '+flist.Strings[lcnt-1]);
         WriteLn;
         WriteLn;

         flist.Clear;
         WriteLn('*FindFirst w/ MatchesMaskList*');
         WriteLn;
         WriteLn('  ff2('+fmask+');');
         ff2(fmask);
         for lcnt := 1 to flist.Count do
           WriteLn('    '+flist.Strings[lcnt-1]);
         WriteLn;
         WriteLn;

         flist.Clear;
         WriteLn('*FindFirst w/ RegExpr - multiple masks capable*');
         WriteLn;
         WriteLn('  ff3('+fmask+');');
         ff3(fmask);
         for lcnt := 1 to flist.Count do
           WriteLn('    '+flist.Strings[lcnt-1]);
         WriteLn;
         WriteLn;
       finally
         if Assigned(flist) then
           FreeAndNil(flist);
       end;
     end;
end.
===== snip =====

-- 
  NOTE: No off-list assistance is given without prior approval.
        Please *keep mailing list traffic on the list* unless
        private contact is specifically requested and granted.



More information about the fpc-pascal mailing list