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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I scan the files in a known Directory? 2

Status
Not open for further replies.

delphiman

Programmer
Dec 13, 2001
422
ZA
I need to scan files in a known Directory.

I should add that these files will have partially known names. Such as
{b]MyFile12345
{b]MyFile67890

Does anyone have any experience on this?
 
hi,

another piece of code:

It fills a listbox with all the txt-files from a directory. change something in the fname that suites you.

procedure fillTree;
var
sr: TSearchRec;
aRecord: string;
stornoPath: string;
fName: string;
begin
ListBox1.Clear;
StornoPath := 'C:\test';
if FindFirst(stornoPath + '\*.TXT', faAnyFile, sr) = 0 then
begin
aRecord := sr.Name;
fName := stornopath + '\' + sr.Name;
ListBox1.Items.Add(aRecord);
while FindNext(sr) = 0 do
begin
aRecord := sr.Name;
fName := stornopath + '\' + sr.Name;
ListBox1.Items.Add(sType + ' ' + aRecord);
end;
FindClose(sr);
end;
if Listbox1.Items.Count > 0 then btnStart.Enabled := True;
end;

Steph [Bigglasses]
 
Hi guys,

Steph's procedure will work perfectly, but I thought it worth the effort to give you my procedure, and it can handle searching subdirectories too:

var
l : TStringList;

procedure SearchTree(APath, ASpec: String; const ASub: Boolean = True);
var
f : TSearchRec;
begin
APath := IncludeTrailingPathDelimiter(APath);
if FindFirst(APath + ASpec, faAnyfile, f) = 0 then
repeat
if f.Name[1] = '.' then Continue;
if ((f.Attr and faDirectory) <> 0) and ASub then
SearchTree(APath + f.Name, ASpec, ASub)
else
l.Add(APath + f.Name);
until FindNext(f) <> 0;
FindClose(f);
end;

That's it. Initialise your TStringList prior to calling the procedure, and it will contain the complete folder and filenames of the matching files. If you set ASub to False it won't go into any subfolders.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top