Link to home
Start Free TrialLog in
Avatar of Patranjali
Patranjali

asked on

To read contents of a folder

Hello all,
Which object in Delphi can I use to read the contents of a folder and then loop thru all the files (of a specified type only)in the folder .I have to then read the contents of the files and save them to different tables.

Thanking in anticipation.
Pat.
ASKER CERTIFIED SOLUTION
Avatar of Cynna
Cynna

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Patranjali
Patranjali

ASKER

Thanks Cynna ,
I am looking into ur note.I have also discovered another way to do it.So lets see...

Pat

Avatar of Wim ten Brink
Short, but simple:

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  SearchRec: TSearchRec;
begin
  if (FindFirst('*.*', faAnyFile - faDirectory, SearchRec) = 0) then begin
    repeat
      WriteLn('Doing something with ', SearchRec.Name);
    until (FindNext(SearchRec) <> 0);
    FindClose(SearchRec)
  end;
end.

One single application that's doing something with all files in the current folder. This is actually the preferred way to use FindFirst/FindNext/FindClose. Cynna already gave a correct answer but that method is out of date. It used to be valid but in an empty folder the FindClose might generate an error since FindFirst won't initialize and SearchRec structure. Also, Cynna's solution also adds folders and you want files only. (Thus, "faAnyFile - faDirectory"...)

Cynna was first however, thus give the points to Cynna. ;-)
Workshop_Alex,

I don't mean to split hair here, but i don't quite agree with your observations:

1. > ... but in an empty folder the FindClose might generate an error since FindFirst won't initialize and SearchRec structure...

Well, I took a quick glance at the RTL system library, and couldn't find this "error scenario":


function FindFirst(const Path: string; Attr: Integer;
  var F: TSearchRec): Integer;
const
  faSpecial = faHidden or faSysFile or faVolumeID or faDirectory;
begin
  F.ExcludeAttr := not Attr and faSpecial;
  F.FindHandle := FindFirstFile(PChar(Path), F.FindData);
  if F.FindHandle <> INVALID_HANDLE_VALUE then
  begin
    Result := FindMatchingFile(F);
    if Result <> 0 then FindClose(F);
  end else
    Result := GetLastError;
end;

procedure FindClose(var F: TSearchRec);
begin
  if F.FindHandle <> INVALID_HANDLE_VALUE then
  begin
    Windows.FindClose(F.FindHandle);
    F.FindHandle := INVALID_HANDLE_VALUE;
  end;
end;


So,

a) If FindFirst() sets INVALID_HANDLE_VALUE, FindClose code is ignored - no harm done.

b) Else, if FindFirst() gets FindMatchingFile(F)<>0, FindClose is called from it. Subsequent
   call to FindClose is redundant, however INVALID_HANDLE_VALUE flag is set, so it's back
   to case a).

If folder is empty FindFirstFile() will return INVALID_HANDLE_VALUE, ie. case a)
So I don't see how can this error be generated here, please correct me if I'm wrong...



2. > Also, Cynna's solution also adds folders and you want files only.

No it doesn't - please take a better look at my code, particularly the line:

> if (SR.Attr <> faDirectory) then OutFullFileNames.Add(BaseDir+SR.Name)


-----------

Now, after setting things straight, I must agree your code is more elegant - I just
copied fragments of some old code I had at hand - didn't spend time cleaning it up though.

> Cynna was first however, thus give the points to Cynna. ;-)

:) Thanks.
Hello all,
Thanks for all you help.But I coulde solve the problem by
using a component called the FileListBox, which can be masked for specific files etc.
So I am  using that component.
Thanks,
Pat
Will someone please delete this question.
Thanks,
Pat
Patranjali:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
Patranjali,
No comment has been added lately (18 days), so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:

RECOMMENDATION: Award points to Cynna http:#7161496

Please leave any comments here within 7 days.

-- Please DO NOT accept this comment as an answer ! --

Thanks,

anAKiN
EE Cleanup Volunteer