• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 255
  • Last Modified:

Get all filenames in a directory

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
aztec
Asked:
aztec
  • 5
  • 5
  • 4
  • +5
1 Solution
 
DrDelphiCommented:
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
 
Stuart_JohnsonCommented:
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
 
Stuart_JohnsonCommented:
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
SteveWaiteCommented:
// 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
 
rfedykCommented:
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
 
shyampaliyathCommented:
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
 
MBoCommented:
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
 
Stuart_JohnsonCommented:
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
 
MoondancerCommented:
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
 
aztecAuthor Commented:
I chose Dr. Delphi's suggestion...
0
 
Stuart_JohnsonCommented:
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
 
aztecAuthor Commented:
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
 
MoondancerCommented:
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
 
MoondancerCommented:
Listening further for response and in the meantime, this for DrDelphi -> qid=20290167
0
 
aztecAuthor Commented:
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
 
DrDelphiCommented:
*Bows to aztec*

Thanks!!
0
 
MoondancerCommented:
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
 
aztecAuthor Commented:
ok, thanks
0
 
Stuart_JohnsonCommented:
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
 
MoondancerCommented:
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
 
DrDelphiCommented:
Stu,
  No problem...never was. Happy coding!!

-DrDelphi
 
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 5
  • 4
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now