Solved

Load a lot of files into RichEdit from a specified Directory

Posted on 2006-06-21
3
212 Views
Last Modified: 2010-04-05
Hello All;

I know there is a better way of doing this, then the way I have "Attempted" to try.

RichEdit1.Lines.LoadFromFile('I:\FolderName\somefile.txt');
RichEdit1.Lines.LoadFromFile('I:\FolderName\somefile_2.txt');
RichEdit1.Lines.LoadFromFile('I:\FolderName\somefile_3.txt');
RichEdit1.Lines.LoadFromFile('I:\FolderName\somefile_4.txt');
RichEdit1.Lines.LoadFromFile('I:\FolderName\somefile_5.txt');

The folder has quite a bit of files located in it.
I need to load ALL The files into the RichEdit1
Basically.
After the 1st file is loaded, then go to the End, and load the 2nd file
Then go to the end of Load the 3rd file, and so on.
With no

Blank spaces between the files.

Any idea's on this one?
I would prefer not to have to write it out like I attempted to do above.

Thanks All;
Carrzkiss
0
Comment
Question by:Wayne Barron
3 Comments
 
LVL 28

Assisted Solution

by:2266180
2266180 earned 25 total points
ID: 16955866
hm ... that is
1) ugly
2) not working.

try soemthing like this:

add all filenames you want to load in a tstringlist named files

RichEdit1.Lines.clear;
s:=TStringList.create;
for i:=1 to files.count do
begin
  s.loadfromfile(files[i-1]);
  RichEdit1.Lines.addstrings(s);
end;
s.free;

if the files are dinamically generated or soemthing, use a findfirst/findnext construction to find them all and add them to the files list.
0
 
LVL 4

Accepted Solution

by:
LMuadDIb earned 100 total points
ID: 16956145
how about TOpenDialog to select the files, its not short and neat but does a good job:

procedure CombineFiles(dlg1: TOpenDialog;Rich1: TRichEdit);

...

procedure TForm1.btn1Click(Sender: TObject);
begin
  // Set up the starting directory to be the current one
  dlgOpen1.InitialDir := GetCurrentDir;
  // Allow multiple files to be selected - of any type
  dlgOpen1.Options := [ofAllowMultiSelect];

  if dlgOpen1.Execute then
  begin
    if dlgOpen1.Files.Count > 0 then
     CombineFiles(dlgOpen1,RichEdit1);
  end;
end;

procedure TForm1.CombineFiles(dlg1: TOpenDialog; Rich1: TRichEdit);
var
  i:  INTEGER;
  s:  ARRAY OF STRING;
  Stream:  TMemoryStream;
begin
  SetLength(s,dlg1.Files.Count);
  for i := 0 to dlg1.Files.Count -1 do
  begin
    // Load files into string array
    if FileExists(dlg1.Files[i]) then begin
      Stream := TMemoryStream.Create;
      try
        Stream.LoadFromFile(dlg1.Files[i]);
        SetLength(s[i], Stream.Size);
        Stream.Read(s[i][1], Stream.Size)
      finally
        Stream.Free
      end
    end
    else s[i] := '';
  end;

  // Send string array results to RichEdit
  Rich1.Clear;
  for i := low(s) to high(s) do
          Rich1.Lines.Append(s[i]);

end;
0
 
LVL 31

Author Comment

by:Wayne Barron
ID: 16956485
Thanks both.

LMuadDIb
I use to have a code like this but had lost it. Thanks a bunch for making me remember it again.

---------
ciuly
1-Ugly  - Yep
2-Not Working - Yep, that is why I posted it and wanted a better solution.
Also, I use to do some like this a long time ago, but with some more added code,
That I just could not remember.


Carrzkiss
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…

730 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