?
Solved

Fill a list from a dir.

Posted on 2003-02-25
17
Medium Priority
?
170 Views
Last Modified: 2010-04-04
'Folder' is a dir like c:\windows
's1' is TListBox.Items
this is the code to fill the listbox with ALL the file op the directory 'folder'

---
procedure TForm1.FillFileList(Folder: string; sl: TStrings);
var Rec : TSearchRec;
begin
 sl.Clear;
 if SysUtils.FindFirst(Folder + '*.*', faAnyFile, Rec) = 0 then
  try
    repeat
      sl.Add(Rec.Name);
    until SysUtils.FindNext(Rec) <> 0;
  finally
    SysUtils.FindClose(Rec);
  end;
end;
---
This code adds only the files from c:\windows.
How can I change the code to add also ALL the sub-directories en sub-files?
0
Comment
Question by:jurgentje
  • 10
  • 6
17 Comments
 
LVL 3

Accepted Solution

by:
ILE earned 120 total points
ID: 8020924
here is the exacly correct solution
i jut tested



one button  one listbox with name s1


procedure TForm1.BitBtn1Click(Sender: TObject);
begin
 s1.items.clear;
 getalldir('c:\idapi\');
end;


procedure tform1.getalldir(s:string);
var rec :tsearchrec;
begin
if sysutils.findfirst(s + '*.*', faAnyFile, Rec)=0 then
begin
   repeat
     if (rec.name<>'.') and (rec.name<>'..') then
     begin
      s1.items.Add(s+Rec.Name);
      if rec.attr=fadirectory then getalldir(s+rec.name+'\');
     end;
   until SysUtils.FindNext(Rec) <> 0;
  sysutils.FindClose(rec);
end;



what are u thin recursion is the power4ful thing :)))


0
 
LVL 3

Expert Comment

by:ILE
ID: 8020935
100 % of working solution


but the dir is not the c:\idapi
but c:\windows\
0
 

Author Comment

by:jurgentje
ID: 8021315
Thanks, i think i can do it with this code ;)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:jurgentje
ID: 8021789
I have a problem with filling the list from a CD.
He gives only the dirs and not the files in this dirs.
0
 
LVL 3

Expert Comment

by:ILE
ID: 8021891
this is because a cd files is read only
0
 
LVL 3

Expert Comment

by:ILE
ID: 8021936
if u do the code exactly how i send to you there will be working on cd

0
 

Author Comment

by:jurgentje
ID: 8021937
I already tought that was the problem, but how do I Change the code?
0
 
LVL 3

Expert Comment

by:ILE
ID: 8021978
i try the code again on the cd work perfectly
0
 

Author Comment

by:jurgentje
ID: 8022001
Not here :( He finds the subdirs, but not the file in thise dirs
0
 

Author Comment

by:jurgentje
ID: 8022014
Not here :( He finds the subdirs, but not the file in thise dirs
0
 

Author Comment

by:jurgentje
ID: 8022028
Not here :( He finds the subdirs, but not the file in thise dirs
0
 

Author Comment

by:jurgentje
ID: 8022046
Not here :( He finds the subdirs, but not the file in thise dirs
0
 

Author Comment

by:jurgentje
ID: 8022054
oeps sorry i refreshed a few times
0
 
LVL 3

Expert Comment

by:ILE
ID: 8022062
i use win98

just the ide instead of use faanyfile  try $FFFF or $FF
just curious what will be happend


replace fanayfile with $ff and try with $FFFF
0
 

Author Comment

by:jurgentje
ID: 8022088
if (rec.attr=17) or (rec.attr=18)  then

this code works
0
 

Expert Comment

by:OneSurfer
ID: 8022396
uses
 FileCtrl

var S: String;
    Rec: TSearchRec;
begin
If SelectDirectory('Select Some Directory','',S) then
If FindFirst(S+'\*.MP3', 0 , Rec) = 0 then with
  ListBox1.Items do begin
  BeginUpdate;
  Repeat
  Add(S+'\'+Rec.Name);
  Until FindNext(Rec) <> 0;
  Endupdate;
end;
{This gives complete path and file name, or just the file name(without the var)If anything, this shows a very simple way to get the Windows select directory dialog. Note: Adds about 700 pathitems in about three secs on older machine and Windows 2000}
0
 

Author Comment

by:jurgentje
ID: 8065805
OneSurfer, this code doesn't take the subdir's
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question