Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 02-13-2003, 09:29 PM
dbassett74 dbassett74 is offline
Senior Member
 
Join Date: Jun 2002
Posts: 395
Default Iterate through directories?

I need to fill a list box with all root directories under a specified parent directory. For example, say I have the following path:

c:\files\data\

I want to only get a list of any "root" directories under the \data\ directory. I do not want to include any sub folder or files. Any help would be appreciated..
Reply With Quote
  #2  
Old 02-13-2003, 10:18 PM
M6450 M6450 is offline
Senior Member
 
Join Date: Feb 2003
Posts: 120
Default RE: Iterate through directories?

Here's the code you need:

var
SR : TSearchRec;
Found : integer;
begin
Found := FindFirst('c:\files\data\*', faDirectory, SR);
while Found = 0 do
begin
ListBox1.Items.Add(SR.Name);
Found := FindNext(SR);
end;
FindClose(SR);
end;


================================================== ================
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." -- Rich Cook
Reply With Quote
  #3  
Old 02-13-2003, 11:09 PM
TD TD is offline
Member
 
Join Date: Jan 2003
Posts: 34
Default RE: Iterate through directories?

Try this one

var
SearchRec : TSearchRec;

///

if FindFirst('c:\files\data\*.*',faDirectory,SearchRe c)=0 then
begin
if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
ListBox1.Items.Add(SearchRec.name);
while FindNext(SearchRec) = 0 do
begin
if (SearchRec.Name <> '.') and (SearchRec.Name<>'..') then
ListBox1.Items.Add(SearchRec.Name);
end;
FindClose(SearchRec);
end;
Reply With Quote
  #4  
Old 02-14-2003, 10:33 PM
dbassett74 dbassett74 is offline
Senior Member
 
Join Date: Jun 2002
Posts: 395
Default RE: Iterate through directories?

It seems that both of the code samples are pulling in the files as well. I only want to pull in directories, not files.
Reply With Quote
  #5  
Old 02-15-2003, 12:26 AM
M6450 M6450 is offline
Senior Member
 
Join Date: Feb 2003
Posts: 120
Default RE: Iterate through directories?

My and his code works as you wanted. It shows only directorys not files. I don't know what you do wrong but it realy works.

================================================== ================
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." -- Rich Cook
Reply With Quote
  #6  
Old 02-15-2003, 09:01 AM
dbassett74 dbassett74 is offline
Senior Member
 
Join Date: Jun 2002
Posts: 395
Default RE: Iterate through directories?

Yes, it doesn't show any files in the child folders, but it does show any files that happen to be in the root folder. An example:

If there are files in c:\root\, they show up. If there are files in c:\root\dir1\, they don't show up.

I'm running WinXP. Is this not consistent with your results?

Again, the code I need is to find any folders that appear in c:\root\. Maybe the code needs some modification??
Reply With Quote
  #7  
Old 02-15-2003, 10:38 AM
M6450 M6450 is offline
Senior Member
 
Join Date: Feb 2003
Posts: 120
Default RE: Iterate through directories?

Oh I forgot. You need to check if the found thing is a file or a dir. I added line that this code won't add "." and ".." to directory list. If you need that just delete "and (SR.Name <> '.') and (SR.Name <> '..')".

var
SR : TSearchRec;
Found : integer;
begin
Found := FindFirst('D:\Pobrane pliki\*', 16 {faDirectory}, SR);
while Found = 0 do
begin
if (SR.Attr = 16 {faDirectory}) and (SR.Name <> '.') and (SR.Name <> '..') then
begin
ListBox1.Items.Add(SR.Name);
end;
found := FindNext(SR);
end;
FindClose(SR);
end;


================================================== ================
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." -- Rich Cook
Reply With Quote
  #8  
Old 04-27-2017, 10:45 PM
Jared Jared is offline
Junior Member
 
Join Date: Dec 2015
Posts: 3
Default

Hi, thanks this is just what I was looking for! Really appreciate it.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 10:49 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2017, vBulletin Solutions, Inc.