Solved

Get all filenames in a directory

Posted on 2002-03-19
21
240 Views
Last Modified: 2010-04-05
Hi...
  Let's say I have a directory and path name stored in a string variable. How can I get *all* the file names that are in that specific directory?

Thanks
   Shawn
0
Comment
Question by:aztec
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 4
  • +5
21 Comments
 
LVL 6

Expert Comment

by:DrDelphi
ID: 6881654
Try this:

Function GetAllfileNames(dir:string):Tstrings;
var list:TstringList;
    srch:TSearchRec;
begin
    if FindFirst(dir+'\*.*',faanyfile,srch)=0 then
    begin
     List:=TStringList.create;
     List.Add(srch.Name);
    end;
    While FindNext(srch)=0 do
    begin
       List.add(srch.name);
    end;
   result:=List;


end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  Listbox1.items:=GetAllFilenames('C:\Winnt');
end;


Good luck!!


0
 
LVL 6

Accepted Solution

by:
Stuart_Johnson earned 0 total points
ID: 6881657
Hi Aztec,

Here is the simplest way I know of:

procedure GetFilesInDirectory(Path: String; var FileList: TStringList);
var
  SR: TSearchRec;
  I: Integer;
begin
  FileList.Clear;
  Path := IncludeTrailingBackslash(Path);
  I := FindFirst(Path + '*.*', faAnyFile, SR);
  while I = 0 do
    begin
      FileList.Add(Path + SR.Name);
      I := FindNext(SR);
    end; {while I = 0}
  FindClose(SR);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  FileList: TStringList;
begin
  FileList := TStringList.Create;
  try
    GetFilesInDirectory('C:\', FileList);
    ShowMEssage(FileList.Text);
  finally
    FileList.Free;
  end;
end;

Hope this helps,

Stu
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 6881658
DAMN!  Sorry guys, I didn't mean to post that as an answer!  It was supposed to be a comment.

My appologies.

Jeeze you have to be quick tho!

Cheers,

Stu
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 3

Expert Comment

by:SteveWaite
ID: 6883172
// something like this?
var FileInfo: TSearchrec;

FindFirst(FromDirectory + '\*.*', faAnyFile, FileInfo);
if FileInfo.Name <> '' then
repeat
  if ((FileInfo.Name[1] <> '.') and (not (FileInfo.Attr and faDirectory > 0)))
    then Addtostringlistorsomething;
until FindNext(FileInfo) <> 0;
FindClose(FileInfo);

Note there are two files '.' and '..' as well in a directory

Similarly ((FileInfo.Name[1] <> '.') and (FileInfo.Attr and faDirectory > 0)) = sub directories

Regards,
Steve

0
 
LVL 1

Expert Comment

by:rfedyk
ID: 6892063
I know that we all like to flex the TSearchRec methods but Windows does it all for you. Create a temporary instance of a TFileListBox, set the Directory property and WHAMMO, Windows delivers all the file names. As a matter of fact, using a TOutlineDirectory, TDirectoryListbox and TFileListbox you can build your own Windows Explorer in a few lines of code.

Roger
0
 
LVL 1

Expert Comment

by:shyampaliyath
ID: 6896135
searchfile: TSearchRec;
filename,src:string;

src:='C:\';

 result:=FindFirst(src+'*.*',$0000003F,searchfile);// This search for first file in the directory stored in the variable 'src' and returns zero if successful.
   {faReadOnly     $00000001     Read-only files
   faHidden     $00000002     Hidden files
   faSysFile     $00000004     System files
   faVolumeID     $00000008     Volume ID files
   faDirectory     $00000010     Directory files
   faArchive     $00000020     Archive files
   faAnyFile     $0000003F     Any file}
   if result=0 then
   begin
        filename:=searchfile.Name;//Searchfile is an object of the TSearchRec which stores the  list filename from the specified directory.
     showmessage(filename);
           result:=findnext(searchfile);// This searches for the next file if present.
        while result=0 do
        begin
          showmessage(filename);
                result:=findnext(searchfile);// This searches for the next file if present.
             filename:=searchfile.Name;
       end;
   end;
0
 
LVL 1

Expert Comment

by:MBo
ID: 6897622
procedure TForm1.Button1Click(Sender: TObject);
var APath:PChar;
begin
APath:='e:\*.*';
ListBox1.Perform(LB_DIR,DDL_READWRITE,Integer(APath));
end;
See another DDL_* values in LB_DIR description
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 6897722
Aztec,

Some feedback would be appreciated.  Please let us know what you're doing before the autograder kicks in.  This question is now eight days old - more than enough time for you to asses whether or not the proposed solutions meet your requirements.

I mentioned previously that I answered this question by mistake.  It is not fair to leave this question locked as other experts won't be able to post a better solution.  If you are happy with my answer, then please accept it, other wise reject the answer so other experts can have a crack at it.

Stuart.
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6939339
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if still open in seven days.  Please post closing recommendations before that time.

Question(s) below appears to have been abandoned. Your options are:
 
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and keep them updated as the collaboration effort continues, to maintain your open and locked questions.  If you are a  KnowledgePro user, use the Power Search option to find them.  Anytime you have questions which are LOCKED with a Proposed Answer which does not serve your needs, please reject it and add comments as to why.  In addition, when you do grade the question, if the grade is less than an A, please add a comment as to why.  This helps all involved, as well as future persons who may access this item for help.

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.20245156.html
http://www.experts-exchange.com/questions/Q.20259219.html
http://www.experts-exchange.com/questions/Q.20263069.html
http://www.experts-exchange.com/questions/Q.20270808.html
http://www.experts-exchange.com/questions/Q.20269981.html
http://www.experts-exchange.com/questions/Q.20277592.html
http://www.experts-exchange.com/questions/Q.20279091.html


To view your locked questions, please click the following link(s) and evaluate the proposed answer.
http://www.experts-exchange.com/questions/Q.20263454.html
http://www.experts-exchange.com/questions/Q.20269773.html
http://www.experts-exchange.com/questions/Q.20279093.html

**** PLEASE DO NOT AWARD THE POINTS TO ME. *****
 
------------>  EXPERTS:  Please leave your closing recommendations if this item remains inactive another seven (7) days.  If you are interested in the cleanup effort, please click this link http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643 
POINTS FOR EXPERTS awaiting comments are listed here -> http://www.experts-exchange.com/commspt/Q.20277028.html
 

Moderators will finalize this question if in @7 days you have not responded.  They will either move this to the PAQ (Previously Asked Questions) at zero points, delete it or awarding expert(s) when recommendations are made, or an independent determination can be made.  Expert input is always appreciated to determine the fair outcome.
 
Thank you everyone.
 
Moondancer
Moderator @ Experts Exchange
0
 

Author Comment

by:aztec
ID: 6943125
I chose Dr. Delphi's suggestion...
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 6943315
aztec,

I'm at a loss.  Firstly, you said you wanted to chose Dr Delphi's suggestion, but you award me the points.  Secondly, Dr Delphi's suggestion and mine are almost identical, so why do you want to award him the points.  And finally, why did you only give this a B grade?  The answer did exactly what you wanted.  I don't care about the points, but I do like having a good answer history.  Can you tell me why my answer wasn't considered to be perfect for what you wanted?

Stu
0
 

Author Comment

by:aztec
ID: 6943458
I wanted to award Dr. Delphi the points, but you had posted a yellow "Proposed Answer", thus there was no "accept as answer" button on Dr. Delphi's, so I just picked yours.

I just needed to finalize this question as it had been lingering for some time....sorry
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6945806
I will correct this for you, aztec, by posting points for DrDelphi.  BUT please tell us why you chose a "B" versus an "A" so we can all understand this a bit better.  Did you find a better solution elsewhere, or was this grade just one of those things?

Thanks,
Moondancer - EE Moderator
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6945810
Listening further for response and in the meantime, this for DrDelphi -> qid=20290167
0
 

Author Comment

by:aztec
ID: 6945819
nope, I used Dr.Delphi's solution to the letter.... I guess I chose a B for Stuart cause he forced my hand with the "Proposed Answer" thing.

Dr.Delphi's answer was perfect - you can give him an "A" !

Thanks
   Shawn
0
 
LVL 6

Expert Comment

by:DrDelphi
ID: 6945981
*Bows to aztec*

Thanks!!
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6946118
Thank you, this has been done -< qid=20290167 >-.

Any time you find that someone has moved your question to the Locked queue with a proposed answer and it does not serve your needs, please reject it and add comments.  This eliminates these types of problems and confusions.

Thank you,
Moondancer - EE Moderator
0
 

Author Comment

by:aztec
ID: 6946349
ok, thanks
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 6946486
Aztec,

I said when I first posted the "answer" that it was a mistake - "DAMN!  Sorry guys, I didn't mean to post that as an answer!  It was supposed to be a comment."  You have a reject answer button.  Why not just use it and award the points to DrDelphi.  Accepting the answer and awarding someone a B or C grade just because they "tied your hands" is just a load of rubbish.

Moondancer,

I didn't realise at the time of my last comment that DrDelphi had infact posted his suggestion before mine.  This means that he should be awarded the points for this question, not me.  I didn't mean to post my comment as an answer (as I've mentioned).  Is there anyway that this question can be have DrDelphi as supplying the correct answer and awarding him the points from this instead of me getting the credit for it?  

DrDelphi,

I really do appologise for stuffing you around like this.  It was certainly unintentional.  Hopefully Moondancer will be able to change this so you're the accepted answerer.

Cheers folks,

Stu.
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6946613
Hi, Stu.  All is well and resolved.  No problems at all.

I posted a separate question to fix this, and DrDelphi has received them.

:) Moondancer - EE Moderator
0
 
LVL 6

Expert Comment

by:DrDelphi
ID: 6947790
Stu,
  No problem...never was. Happy coding!!

-DrDelphi
 
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses
Course of the Month5 days, 8 hours left to enroll

626 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