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

can somebody tell me how to get a list of all files in a dir??

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
can somebody tell me how to get a list of all files in a dir??
 
Ey dude,

The procedure listed under here finds all the files
(everything with an extension). If you also want to
find directories then you must remove both the
if .. then statements. In this procedure they
(filenames WITHOUT paths) are automatically added to
a combobox.

Code:
procedure TForm1.FindAllFiles(Sender: TOBject);
var sr: TSearchRec;
begin
 FindFirst(<YOUR PATH HERE>+'\*.*',(faAnyFile),sr);
  begin
   if ExtractFileExt(sr.FindData.cFileName) <> '' then
   Combobox1.Items.Add(sr.FindData.cFileName);
  end;

  while FindNext(sr) = 0 do
  begin
   if ExtractFileExt(sr.FindData.cFileName) <> '' then
   Combobox1.Items.Add(sr.FindData.cFileName);
  end;
 FindClose(sr);
end;

Hope this helps,

BobbaFet Everyone has a right to my opinion.
E-mail me at cwcon@programmer.net
 
Your in luck you have a second example.

Good Luck.

{ The CLasses unit is required in the Uses clause
Sample call:

Be sure you have a TStrings component initialized before making this call:
Var MyDocuments : TStrings;
MyDocuments := TStringList.create;

GetFileList(MyDocuments,'c:\my documents\*.doc',faAnyFile);

FYI:
Constant Value Description
faReadOnly $00000001 Read-only files
faHidden $00000002 Hidden files
faSysFile $00000004 System files
faVolumeID $00000008 Volume ID files
faDirectory $00000010 Directory files
faArchive $00000020 Archive files
faAnyFile $0000003F Any file

MyDocuments will hold all path and filenames which match *.doc in the C:\My Documents directory
To recurse through all files:
for x := 0 to MyDocuments.count-1 Do
aString := MyDocuments[x] ;

}

function GetFileList(aFiles : TStrings; aPath : String; aFileAttribute : Integer) : boolean;
Var aSearchRec : TSearchRec;
begin
UpperCase(aPath);
If FindFirst(aPath,aFileAttribute,aSearchRec) = 0 then
begin
aFiles.Add(ExtractFilePath(aPath)+UpperCase(aSearchRec.Name));
While FindNext(aSearchRec) = 0 Do
aFiles.Add(ExtractFilePath(aPath)+UpperCase(aSearchRec.Name));
FindClose(aSearchRec);
end
Else
begin
GetFileList := false;
FindClose(aSearchRec);
end;
end;
 
Or drop a filelistbox component on your form set the dir and extract the filenames from the 'items' property. it's in the W3.1 components pallet.!
The 'items' Help example is a bit over the top, but will show you how to use it.

Steve.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top