Board index » delphi » Getting List of Files in Specified Directory

Getting List of Files in Specified Directory

I need to access file names in a given directory without user
interaction during run time

i.e.       *.txt   in directory c:\temp\

and be able to access these file names (string)  in an array.

I am relatively new to Delphi and I am slowly beginning to pick it up (I
hope).  I think that I need to use an OpenDialog component and
instantiate it during runtime.  Set the filter property to *.txt,
execute it,  and I think the array that I am looking for should be
created somewhere.

I have been unsuccessful getting the syntax correct to perform the
described logic.  Is this the right approach ? Could someone help me
with the proper code syntax and array reference?

Thanks in advance,

Lou

 

Re:Getting List of Files in Specified Directory


Quote
Lou wrote:
> I need to access file names in a given directory without user
> interaction during run time

Try the following code.  It finds all files in a given directory based on a mask and returns a TStringlist.

unit FileUtil1;

interface

uses
  Windows, Classes, SysUtils;

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  procedure MakeFileList(var List : TStringList; Dir, Mask : string);
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

implementation

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
procedure MakeFileList(var List : TStringList; Dir, Mask : string);
{ finds filenames in dir using mask and adds to a stringlist }
var
  SearchRec: TSearchRec;
begin
  try
     { add  '\ ' if needed to end of directory path }
    if Dir[Length(Dir)] <> '\' then
     Dir := Dir + '\';

      { if no files then exit }
    if FindFirst(Dir + Mask, faAnyFile, SearchRec) <> 0 then
    begin
      FindClose(SearchRec);//releases memory allocated by FindFirst
      exit;
    end;

      { add first name to list }
    List.add(searchrec.name);

      { continue to find files until no more }
    while FindNext(SearchRec) = 0 do
     List.add(SearchRec.name);

  finally
    FindClose(SearchRec);//releases memory allocated by FindFirst
  end;//try
end;//MakeFileList

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

end.

--

Wayne Vinson

for FreeWare samples (with source) see:
http://www.geocities.com/TimesSquare/Bunker/4739/

Re:Getting List of Files in Specified Directory


Quote
In article <36D9B418.E6A08...@erols.com>, Lou wrote:
> I need to access file names in a given directory without user
> interaction during run time

> i.e.       *.txt   in directory c:\temp\

> and be able to access these file names (string)  in an array.

What you want is a stringlist and to use findfirst/findnext

var
  sl : TStringList ;
  SearchRec: TSearchRec;
  theDir : string ;
  a : integer ;
begin
  theDir  := 'c:\some\path\'
  sl := TStringList.Create ;
  try
    a = FindFirst(theDir+'*.txt', faAnyFile, SearchRec);
    if a = 0 then
    begin
      sl.add(theDir.Searchrec.Name) ;
      while FindNext(SearchRec) = 0 do
        sl.Add(theDir+ SearchRec.Name) ;
      FindClose(searchRec);
    end;

  {the stringlist now contains the full path/filenames and you can do stuff
with it}

  finally
    sl.free ;
  end;
end;

--
 The Taz!

Re:Getting List of Files in Specified Directory


Quote
In article <36D9D4BF.6C138...@geocities.com>, Wayne Vinson wrote:
> { if no files then exit }
>     if FindFirst(Dir + Mask, faAnyFile, SearchRec) <> 0 then
>     begin
>       FindClose(SearchRec);//releases memory allocated by FindFirst
>       exit;
>     end;

If FindFirst fails, FindClose should not be called.

 Mike Orriss (m...@3kcc.co.uk)
 http://www.3kcc.co.uk/notetree.htm

Other Threads