Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search Filter 3

Status
Not open for further replies.

abcfantasy

Programmer
Jun 3, 2006
110
0
0
DK
The following code:

FindFirst( Path + '*.mp3', faAnyFile - faDirectory, ... )

will return all mp3 files located in Path.

The question is, what do I have to do, if for example, I want to find mp3, wav and wma files?

Thanks
Andrew

ABC -
 
You have to construct a loop using 'FindNext', there are several examples of this in the forum.
To find other file types make a function and pass the required extension into it. So you search for each consequtivly and store the results in a list. I don't think there is any way of using 2 or more wild cards at the same time.





Steve: Delphi a feersum engin indeed.
 
Hi!

var
n:integer;
s:tsearchrec;
k:tstrings;

begin

n := findfirst('c:\*.mp3',faanyfile,s);
if n=0 then begin
listbox1.items.Add(s.Name);
repeat
n := findnext(s);
if n = 0 then listbox1.items.Add(s.Name);
until n<>0;
end;

n := findfirst('c:\*.wav',faanyfile,s);
if n=0 then begin
listbox1.items.Add(s.Name);
repeat
n := findnext(s);
if n = 0 then listbox1.items.Add(s.Name);
until n<>0;
end;

n := findfirst('c:\*.wma',faanyfile,s);
if n=0 then begin
listbox1.items.Add(s.Name);
repeat
n := findnext(s);
if n = 0 then listbox1.items.Add(s.Name);
until n<>0;
end;
{****************************************}

OSP
 
Thanks for your help. I thought there could be a way to include more than 1 wildcard in the FindNext.

The best way I think will be a procedure that takes the path as a parameter.

Thanks again
Andrew

ABC -
 
Ok another question..

I'll have to create a property for entering the list of filters. What type should I make the property to be so that I could easily read the different filters?

ABC -
 
I'm always using this function:
Code:
// Takes a delimited string and returns the n'th field
function Parse(str: String; delimiter: Char; param_num: Integer): String;
var
  c, x, y : Integer;
begin
  x := 1; // param number that we're currently 'in'
  y := 0; // position of previous delimiter 
  for c := 1 to length(str) do
    if str[c] = delimiter then  // this char is a delimiter
    begin
      if x = param_num then
        Break;
      inc(x);
      y := c;
    end;
  if x = param_num then
    Parse := copy(str, y + 1, c - y - 1)
  else
    Parse := '';
end;

It means you could set up your filter property to take a string such as '*.mp3,*.wav,*.wma' and use the Parse function in your loop. ie.
Code:
procedure AddFilters(AFilters: String);
var
  c : Integer;
  s : String;
  f : TSearchRec;
begin
  c := 1;
  s := Parse(AFilters, ',', c);
  while s <> '' do
  begin
    if FindFirst(s, faAnyFile, f) = 0 then
      repeat
        ListBox1.Items.Add(f.Name);
      until FindNext(f) <> 0;
    FindClose(f);
    inc(c);
    s := Parse(AFilters, ',', c);
  end;
end;
 
I suggest you use the CommaText property of TStringList.

This little known property is very useful in the situation described by abcfantasy.

Create a string list and assign your file extenstions to the CommaText property:
Code:
var
  exts: TStringList;
begin
  exts := TStringList.Create;
  exts.CommaText := '*.mp3,*.wav,*.wma';
...
You can then access individual file extensions quite simply:
Code:
  ShowMessage( exts[0] );   // displays *.mp3
  ShowMessage( exts[2] );   // displays *.wma

Andrew
Hampshire, UK
 
I didn't know about CommaText - very useful.

I often need to process a string delimited by Tab characters, or spaces - which is where I'd fall back to using the Parse function above.
 
Thanks towerbase, that is exactly what I needed :D

And thanks to griffyn too. I guess it could be useful for other delimiters.

Thank you once again
Cheers
Andrew

ABC -
 
And thanks to griffyn too. I guess it could be useful for other delimiters.

in fact TStringlist supports ANY delimiter.
look at the DelimitedText property.

straight from delphi help:

Code:
Use DelimitedText to get or set all the strings in the TStrings object in a single string. 
When reading DelimitedText, the resulting value delimits individual strings in two ways: each string is surrounded (before and after) by the quote character specified by the QuoteChar property. In addition, individual strings are separated by the character specified by the Delimiter property. 
When writing DelimitedText, individual strings must be separated using QuoteChar at both ends, using Delimiter as a separator, or using both these methods.
 
Note: CommaText is the same as the DelimitedText property when Delimiter is ',' and QuoteChar is '"'.

Cheers,
Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Good stuff whosrdaddy, the end is near for my little Parse function :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top