PDA

View Full Version : Searching Question.



Voltrox
14-12-2006, 11:51 PM
Hello,

1. Is there any way that I make my Delphi program get the names of all the files in a directory?

2. Is there a search string function that can search for keywords in a string?

Thank you.

jdarling
15-12-2006, 03:05 AM
In short, yes and yes.

In long, look at findfilefirst in the delphi or windows API help files, look at the pos function or search for regex and delphi.

Remember, your best friends are the help file, google, google code search, and the borland news groups. All of the fore-mentioned have excessive amounts of info about the topics if you had looked there first.

If you have something more advanced, or don't understand a specific aspect of the above search first then ask again.

cronodragon
15-12-2006, 03:47 AM
As jdarling told you, Delphi has a good example on FindFirst, which is one of the functions used to search for files:

procedure TForm1.Button1Click(Sender: TObject);

var
sr: TSearchRec;
FileAttrs: Integer;
begin
StringGrid1.RowCount := 1;
if CheckBox1.Checked then
FileAttrs := faReadOnly
else
FileAttrs := 0;
if CheckBox2.Checked then
FileAttrs := FileAttrs + faHidden;
if CheckBox3.Checked then
FileAttrs := FileAttrs + faSysFile;
if CheckBox4.Checked then
FileAttrs := FileAttrs + faVolumeID;
if CheckBox5.Checked then

FileAttrs := FileAttrs + faDirectory;
if CheckBox6.Checked then
FileAttrs := FileAttrs + faArchive;
if CheckBox7.Checked then

FileAttrs := FileAttrs + faAnyFile;

with StringGrid1 do
begin
RowCount := 0;

if FindFirst(Edit1.Text, FileAttrs, sr) = 0 then

begin
repeat
if (sr.Attr and FileAttrs) = sr.Attr then
begin
RowCount := RowCount + 1;
Cells[1,RowCount-1] := sr.Name;
Cells[2,RowCount-1] := IntToStr(sr.Size);
end;
until FindNext(sr) <> 0;
FindClose(sr);
end;
end;
end;

For the 2nd question, you can also use PosEx, which lets you set the starting position of the search.

Voltrox
15-12-2006, 01:11 PM
Thank you very much. I'll try that now, and won't bother you again unless necessary. :)

Very helpful.

Voltrox
16-12-2006, 01:33 AM
I don't understand the part about Edit1.text?

How does that code work?

WILL
18-12-2006, 02:41 AM
Edit1.Text is just a string. Edit1 is the default name for a TEdit component in a Delphi application.

I don't know if the newer (higher than 7/2005) editions of Delphi still have the handy documentation, but if you want to know more about the VCL (Visual Component Library) I recommend looking through the Help files.

Just make a new application and play with the component bar, dropping different components onto your form and see what type of components they are. Then got to Help -> Delphi Help then a little dialog box will popup. Go to the 'Index' tab and type in the name of the function or type that you want to learn more about. Most of everything you'll want to know about the VCL or the language syntax will be listed in there.

If you cannot find it there, you can altenately use the 'Find' tab and see if information in another topic heading will hold any insight to what you need to know.

Hope this helps.