AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

read from multiple Files

Ein Thema von mohfa · begonnen am 25. Mär 2009 · letzter Beitrag vom 26. Mär 2009
Antwort Antwort
mohfa

Registriert seit: 11. Feb 2007
97 Beiträge
 
Delphi 7 Enterprise
 
#1

read from multiple Files

  Alt 25. Mär 2009, 17:23
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
  Mit Zitat antworten Zitat
alzaimar
(Moderator)

Registriert seit: 6. Mai 2005
Ort: Berlin
4.956 Beiträge
 
Delphi 2007 Enterprise
 
#2

Re: read from multiple Files

  Alt 25. Mär 2009, 17:54
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.
"Wenn ist das Nunstruck git und Slotermeyer? Ja! Beiherhund das Oder die Flipperwaldt gersput!"
(Monty Python "Joke Warefare")
  Mit Zitat antworten Zitat
Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.139 Beiträge
 
Delphi 12 Athens
 
#3

Re: read from multiple Files

  Alt 25. Mär 2009, 18:06
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;
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests
  Mit Zitat antworten Zitat
mohfa

Registriert seit: 11. Feb 2007
97 Beiträge
 
Delphi 7 Enterprise
 
#4

Re: read from multiple Files

  Alt 25. Mär 2009, 19:09
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;
  Mit Zitat antworten Zitat
Muetze1
(Gast)

n/a Beiträge
 
#5

Re: read from multiple Files

  Alt 26. Mär 2009, 07:31
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;
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 00:12 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