![]() |
read from multiple Files
Hi every body .
I have this Code :
Delphi-Quellcode:
This Loads only One File into FS ;
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; But if i want to load many files into FS how does it be possible ??? i tried to do the Following :
Delphi-Quellcode:
But it doesn't work
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 . . . . Many thanks |
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:
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.
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; HTH. |
Re: read from multiple Files
Zitat:
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:
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; |
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; |
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 02:49 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz