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!

Finding files in subdirectories

Status
Not open for further replies.

Brabrabra

Programmer
Mar 12, 2002
6
NL
Can anyone give me a tip on how I can let a Delphi program find the subdirectories of a certain dir and find files in them?

Thanks
 
Use
SetCurrentDir - set to the directory you wanna search



FindFirst - Searches for the first instance of a file/directory with a given set of attributes in a specified directory

FindNext - Returns the next entry matching the name and attributes specified in a previous call to FindFirst
 
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;
 
You have to build a function which calls itself recursively.

I have a routine like this somewhere... I will post it if i find it...
 
> 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;

Something like that anyway.

lou
 
Replace
if SearchRec.Attr = faDirectory then
in the above code with
if (SearchRec.Attr and faDirectory) > 0 then
 
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.
 
hi rfedyk

I'm interested in your method but I'm having trouble recursing through the directories, can you give some pointers?

At the mo. I'm using OpenDirectory but how does one recurse through each directory until all the sub dirs have been read.

lou
 
Hi Weez,

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;

Roger Fedyk
 
Great! It works. As far as I know it only needed
FileListBox1.Directory:=dirname;
after
DirectoryListBox1.Directory:=dirname;

Thank you very much rfedyk! :)
 
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?

Steve.
 
Steve,

Sounds like a project for some enterprising programmer.

Roger
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top