Hopefully this code will help you, its something I worte which searches for all files in a given directory. Hope this is what you need!
procedure TForm1.FindEm(Sender: TObject);
var
SearchRec: TSearchRec;
Path : String;
begin
Listbox1.Items.Clear;
Path := 'C:\Temp';
if FindFirst(Path + '\*.*', faSysFile, SearchRec) = 0 then
begin
Listbox1.Items.Add(SearchRec.Name);
while FindNext(SearchRec) = 0 do
begin
Listbox1.Items.Add(SearchRec.Name);
end;
end;
end;
> EricDraven (Programmer) Mar 13, 2002
> Hopefully this code will help you, its something I worte
> which searches for all files in a given directory. Hope
> this is what you need!
Thanks! I can use this. But it doesn't search in the subdirectories yet, so I still have to find something for that.
When I use the FindFirst() function and I put faDirectory in the attr field, it not only finds directories, but also normal files. How can the program know what's a file and what's a directory?
if FindFirst(Path + '\*.*', faDirectory, SearchRec) = 0 then
begin
if SearchRec.Attr = faDirectory then
Showmessage(SearchRec.name +' is a directory');
end;
There's a simple way to do this without using TSearchRec code.
Create a local instance of a DirectoryListBox and a FileListBox setting their Visible properties to false. Assign the FileListBox to the DirectoryListBox.FileList property. Assign your subdirectory name to the DirectoryListBox.Directory property and set the FileListBox.Mask property. Windows will do all the work for you.
We need to add another component to let you do that easily. Create an instance of TDirectoryOutline which is in the Samples palette.
The following code will give you everything that Explorer sees:
procedure BuildTree;
var DirName,FilName:string;
Loop,PrevIC,FLoop:integer;
begin
XDirectoryOutline1.Directory:='c:\';
{ TDirectoryOutline appears to have a bug in its
implementation of FullExpand so the following
REPEAT loop does the full expand instead}
repeat
PrevIC:=XDirectoryOutline1.ItemCount;
for Loop:=1 to XDirectoryOutline1.ItemCount do
XDirectoryOutline1.Items[Loop].Expand;
until XDirectoryOutline1.ItemCount=PrevIC;
//we now have all the directory names
for Loop:=1 to XDirectoryOutline1.ItemCount do
begin
{FullPath returns the directory name with '\\'
after the drive letter, so we need to remove the
extra '\'}
dirname:=XDirectoryOutline1.Items[Loop].FullPath;
dirname:=copy(dirname,1,3)+copy(dirname,5,255);
{set the DirectoryListBox directory}
DirectoryListBox1.Directory:=dirname;
{we now have all files for that directory}
for FLoop:=1 to FileListBox1.Items.Count do
if UpperCase(DirName)='C:\' then
FilName:=dirname+FileListBox1.Items[FLoop-1]
else
FilName:=dirname+'\'+FileListBox1.Items[FLoop-1];
{do what ever you want with the file name}
end;
end;
Nice to see I am not the only one who would think of using the supplied components before trying to drive findfirst() etc.
A word of caution is that these components are on the W3.1 Pallette (I think that TDirectoryoutline is a decendent of this group as well), this means that future versions of windows might not support them?
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.