Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi read from multiple Files (https://www.delphipraxis.net/131489-read-multiple-files.html)

mohfa 25. Mär 2009 17:23


read from multiple Files
 
Hi every body .

I have this Code :

Delphi-Quellcode:
Procedure Load(const AFilename: String);
var
  MS : TMemoryStream;
  FS : TFileStream;

begin

    if FileExists(AFilename) then
    begin
      MS := TMemoryStream.Create;
      try
        FS := TFileStream.Create(AFilename, fmOpenRead or fmShareDenyNone);
        try
          MS.Size := FS.Size;
          FS.Position := 0;
          MS.Position := 0;
          MS.CopyFrom(FS, 0);
        finally
          FS.Free;
        end;
      finally
        MS.Free;
      end;
    end;
  end;
This Loads only One File into FS ;

But if i want to load many files into FS how does it be possible ???

i tried to do the Following :

Delphi-Quellcode:
Procedure Load(const AFilename: String);
var
  MS : TMemoryStream;
  FS : TFileStream;
MyStringList:TStringList;
i:integer;
begin
MyStringList:=TStringList.Create;
For i:=0 to MyStringList.count-1 do
if FileExists(MyStringList[i]) then
.
.
.
.
But it doesn't work


Many thanks

alzaimar 25. Mär 2009 17:54

Re: read from multiple Files
 
Hi,

I'm sorry, I don't know what you want. Your code sample does not show what you tried and what you want to achieve. Do you want to load the contents of many file into one stream? This can be achieved by adding the contents to the end of the memory stream.
Delphi-Quellcode:
Procedure AddFileToStream (aStream : TStream; aFile : String);
Var
  filestream : TFileStream;

Begin
  filestream := TFileStream.Create (aFile, fmOpenRead);
  Try
    aStream.CopyFrom (filestream, filestream.Size);
  Finally
    filestream.Free;
  End
End;
Btw: In your code sample, you don't have to set any of the 'Size' or 'Position' properties. This is done by the constructor and the CopyFrom method, by setting the Size-parameter to 0.

HTH.

himitsu 25. Mär 2009 18:06

Re: read from multiple Files
 
Zitat:

Zitat von alzaimar
Btw: In your code sample, you don't have to set any of the 'Size' or 'Position' properties. This is done by the constructor and the CopyFrom method, by setting the Size-parameter to 0.

Delphi-Quellcode:
Procedure Load(const AFilename: String);
var
  MS : TMemoryStream;
  FS : TFileStream;

begin
    if FileExists(AFilename) then
    begin
      MS := TMemoryStream.Create;
      try
        FS := TFileStream.Create(AFilename, fmOpenRead or fmShareDenyNone);
        try
          //FS.Position := 0;
          //MS.Position := 0;
          MS.CopyFrom(FS, FS.Size);
        finally
          FS.Free;
        end;
      finally
        MS.Free;
      end;
    end;
  end;
Zitat:

Delphi-Quellcode:
MyStringList:=TStringList.Create;
// string list contains no files. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
For i:=0 to MyStringList.count-1 do
  if FileExists(MyStringList[i]) then

Delphi-Quellcode:
MS := TMemoryStream.Create;
try
  For i:=0 to MyStringList.Count-1 do
    if FileExists(MyStringList[i]) then
    begin
      FS := TFileStream.Create(MyStringList[i], fmOpenRead or fmShareDenyNone);
      try
        MS.CopyFrom(FS, FS.Size);
      finally
        FS.Free;
      end;
    end;
  ...
finally
  MS.Free;
end;

mohfa 25. Mär 2009 19:09

Re: read from multiple Files
 
I did it but that doesn't give me the result i'm looking for ( i want to load many files into the Stream ).

So what i did id :

Delphi-Quellcode:
Procedure Load(const AFilename: String);
var
  MS : TMemoryStream;
  FS : TFileStream;
  MyStringList:TStringList;
  i:integer;
begin
   MS := TMemoryStream.Create;
   MyStringList:=TStringList.Create;
   MyStringList.Add(AFilename);
try
  For i:=0 to MyStringList.Count-1 do
    if FileExists(MyStringList[i]) then
    begin
      FS := TFileStream.Create(MyStringList[i], fmOpenRead or fmShareDenyNone);
      try
        MS.CopyFrom(FS, FS.Size);
      finally
        FS.Free;

      end;
    end;

finally
// All the MS contents will be saved to Res.txt as an example
  MS.SaveToFile('Res.txt');
  MS.Free;
end;
end;
Delphi-Quellcode:
// as an example how to use it
procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
begin
{FilesList : is a TListBox
items[0]:=c:\001.txt
items[1]:=c:\002.txt
.
.
.
}

for i:=0 to FilesList.Items.Count-1 do
Load(FilesList.Items[i]);
end;

Muetze1 26. Mär 2009 07:31

Re: read from multiple Files
 
Hi!

In your Load function, you create a new stringlist. Then you just add one filename to this stringlist. After this, you iterate through all items of the stringlist (everytime just one) and you are wondering why it is still just one entry?

Delphi-Quellcode:
Procedure Load(const AFilenames: TStrings);
var
  MS : TMemoryStream;
  FS : TFileStream;
  i:integer;
begin
  MS := TMemoryStream.Create;
  try
    For i:=0 to AFilenames.Count-1 do
    begin
      if FileExists(AFilenames[i]) then
      begin
        FS := TFileStream.Create(MyStringList[i], fmOpenRead or fmShareDenyNone);
        try
          MS.CopyFrom(FS, FS.Size);
        finally
          FS.Free;
        end;
      end;
  finally
      // All the MS contents will be saved to Res.txt as an example
    MS.SaveToFile('Res.txt');
    MS.Free;
  end;
end;
Delphi-Quellcode:
// as an example how to use it
procedure TForm1.Button1Click(Sender: TObject);
var
  i:integer;
begin
  {FilesList : is a TListBox
   items[0]:=c:\001.txt
   items[1]:=c:\002.txt
   .
   .
   .
    }

  Load(FilesList.Items);
end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 00:50 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz