How to select a 'range' of files to process

What I need to do is be able to have a user enter a 'range' and then based on what they enter fill a listbox with the files in the directory that qualify against the entered range.

For example here are some sample files that would be in the directory at any given time:
AA01070001
AA01080004
AB03180001
AB03180003
AB04290028
AC04290029
AC04290035
BA04290036
BA06030001
BB06030002
CA07220003
CB07220004

Some directories have files that include 'alpha' characters and some are numeric only.

One range query example would be: AA*-AB*   which I'd like to return all files that match AA or AB
Another example: AA*-CA*  which I'd like to return everything except the CB07220004 file.

The 'wildcard' symbol I'd need to be flexible with like in the following example:
AA0108*-CA*   which would leave out the AA01070001

I'm not familar if there is a Delphi component out there that does this type of thing or what method of coding this would be the best way without too much effort. If you need more clarification, let me know and I'll try to explain what I need to do further.

Thanks in advance for any time spent on this. I appreciate it!
JD_Steele2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JohnjcesCommented:
Here is a procedure for you that will do what you need.

It uses a listbox to hold the files found and uses the * as a wildcard.

InDir = Subdirectoroies. If True will search subdirectories under your top search directory.

Pathname will be your direcotry path, i.e. C:\My Files\Stuff

FileName will be AA123* or whatever you are wanting to find.

StopIt is a global boolean and if set to true outside the procedure, like a button click Stop := True will stop the search.

And, this is off the top of my head..... so may need some tweaking!

John
procedure FileSearch(const PathName, FileName : string; const InDir : boolean);
var
 Rec  : TSearchRec;
 Path : string;
begin
 Path := IncludeTrailingPathDelimiter(PathName);
 Application.ProcessMessages;
 if FindFirst(Path + FileName, faAnyFile - faDirectory, Rec) = 0 then
  try
    repeat
     Application.ProcessMessages;
      if StopIt then
       Exit;
      ListBox1.Items.Add(Path + Rec.Name);
      Application.ProcessMessages;
    until FindNext(Rec) <> 0;
  finally
    FindClose(Rec);
 end;
 
 If not InDir or StopIt then
  Exit;
 
 if FindFirst(Path + '*.*', faDirectory, Rec) = 0 then
  try
   repeat
    if ((Rec.Attr and faDirectory) <> 0)  and (Rec.Name<>'.') and (Rec.Name<>'..') then
      FileSearch(Path + Rec.Name, FileName, InDir);
     Application.ProcessMessages;
   until FindNext(Rec) <> 0;
  finally
   FindClose(Rec);
  end;
end; 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
diniludCommented:
Try this

procedure FileSearch(const PathName, FileName : string; List : TStrings;
                   FilterFrom,FilterTo:String);
var
 Rec  : TSearchRec;
 Path : string;
 Filename1:String;
 i,l,lFrom,lTo,Diff1,Diff2:Integer;
 Valid:Boolean;
begin
 if FilterFrom='' then FilterFrom:=char(1);
 if FilterTo='' then FilterTo:=char(255);
 FilterFrom:=UpperCase(FilterFrom);
 FilterTo:=UpperCase(FilterTo);
 l:=Max(length(FilterFrom),length(FilterTo));
 lFrom:=length(FilterFrom);
 lTo:=length(FilterTo);
 Path := IncludeTrailingPathDelimiter(PathName);
 if FindFirst(Path + FileName, faAnyFile - faDirectory, Rec) = 0 then
  try
    repeat
      Filename1:=UpperCase(Rec.Name);
      Valid:=True;
      Diff1:=0; Diff2:=0;
      For i:=1 to l do
      begin
        if Diff1=0 then Diff1:=Ord(Filename1[i])-Ord(FilterFrom[i]);
        if i<=lFrom then
        begin
           if Diff1<0 then
           begin
              Valid:=False;
              Break;
           end;
        end else Diff1:=1;
        if Diff2=0 then Diff2:=Ord(FilterTo[i])-Ord(Filename1[i]);
        if i<=lTo then
        begin
           if Diff2<0 then
           begin
              Valid:=False;
              Break;
           end;
        end  else Diff2:=1;
        if (Diff1>0) and (Diff2>0) then Break
      end;
      if Valid then List.Add(Path+Rec.Name);
    until FindNext(Rec) <> 0;
  finally
    FindClose(Rec);
 end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Edit1.Text:='C:\';
  Edit2.Text:='A';
  Edit3.Text:='P';
  ListBox1.Clear;
  FileSearch(Edit1.Text,'*.*',ListBox1.Items,Edit2.Text,Edit3.Text);
end;
0
JD_Steele2Author Commented:
I used a combination of both ideas, so I split the points evenly. Thanks and sorry it took me so long to try this out. Dang flu bug knocked me out for a few days and had to play 'catch-up' on a bunch of other stuff. Thanks again.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.