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!

List Directories in a Directory

Status
Not open for further replies.

StevenK

Programmer
Jan 5, 2001
1,294
GB
I'm probably doing something fundamentally wrong here but I can't seem to determine a list of folders within another folder on the local machine. I've been able to successfully use the 'FindFirst()' function to obtain a list of files of a specific type (for instance with the extension '.txt') but can't seem to obtain a list of folders. I've tried but to no avail. If I use the code 'FindFirst('C:\Folder1', faDirectory, tpSearchRec)' I only get the list 'Folder1'. I've tried variations on a theme here but seem to have no luck. Does anyone have a fool-proof (and idiot-proof) solution to this one.
Thanks in advance.
Steve
 
Hi Steve

There may be moans from some quaters here, but I would use a directory listbox (W3.1). It dosnt have to be visible.
Find first etc, is certainly not 'Idiot proof' in my opinion.

Steve.
 
Hi StevenK,

If you want ALL the directories you need to use FindFirst,
and loop FindNext until you have them all! Here is a piece of code I use :

procedure FindAllDirectories(sender: TObject)
var sr: TSearchRec;
begin
FgindFirst(DefPath.Text+'\*',(faDirectory),sr);
if ExtractFileExt(sr.FindData.cFileName) = '' then
Edit16.Items.Add(sr.FindData.cFileName);

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


I hope this helps,

BobbaFet Everyone has a right to my opinion.
E-mail me at cwcon@programmer.net
Be a nice guy 'n press that little ol' button
VVV---(this one) for me ;-)
 
Whoopsy, there is a small error in the code I gave you.
The DefPath.Text+'\*'[\b] Should be the path to the directory you want! Sorry about that but I forgot to adjust
that part for you :)

BobbaFet Everyone has a right to my opinion.
E-mail me at cwcon@programmer.net
Be a nice guy 'n press that little ol' button
VVV---(this one) for me ;-)
 
The test
Code:
if ExtractFileExt(sr.FindData.cFileName) = ''
in the code above will add extensionless files as well as directories.

To determine whether a file is a directory, use instead
Code:
if (sr.Attr and faDirectory > 0)
 
hi MikeEd,

Thanks for that piece of code, I knew about that problem,
but seeing how many people actually have extensionless files
on their hard disk its not a big problem, but now I can get it to work properly. Thanks !!!

BobbaFet Everyone has a right to my opinion.
E-mail me at cwcon@programmer.net
Be a nice guy 'n press that little ol' button
VVV---(this one) for me ;-)
 
Hi BobbaFet

Just to prove to myself that 'idiot-proof' dosnt include myself, I wrote a routine this morning to back up all the files in a folder to our archive drive using Findfirst and Findnext, only had to find files and not folders.
It almost had me when I found that a searchrec.name result dosnt include the full path!!.
This had to be User-proof as well so it includes 3 layers of Validation and exception handling.

QED is a Latin phrase, roughly 'Point Proven'!!.

Steve.




 
Thanks for the help with this problem. Now have the code operating as required.
Steve
 
Well Steve (sggaunt),

that's way i use an edit box to keep the base path in :)
I also use a dropdown list to store directories in so i can
loop through it :) (I could put it in arrays but I like working with objects a whole lot better, dont ask why ...)

Then i just use the base path and the dropdown list to concat them into a new base path :) worx like a charm :)


BobbaFet

BobbaFet Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top