Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Cargar Varios CSV's en un solo Memo (https://www.clubdelphi.com/foros/showthread.php?t=53160)

Albertito 12-02-2008 18:32:51

Cargar Varios CSV's en un solo Memo
 
Hola a todos, soy nuevo en el foro y también llevo poco programando en Delphi, así que de antemano ya agradezco cualquier ayuda que me puedan dar al respecto.

Necesito cargar varios archivos CSV en un memo, por ejemplo, o algo que luego me permita tratar los valores para poder hacer las gráficas con un TChart.
Los archivos son del tipo:
C:\200801220930_Demanda_Inst.csv
C:\200801220935_Demanda_Inst.csv
C:\200801220940_Demanda_Inst.csv
Con un archivo me funciona bien y grafico los valores en variables del tipo:
Código Delphi [-]
 
type
MesuresZones = Record
 Nom:TStringList;
 Data:TStringList;
 Mesura:TStringList;
Mi problema es poder tratar toda la información de todos los CSV's juntos y así que en las variables generadas tipo MesuresZones ya pueda dibujar las gráficas. El LoadFromFile o LoadFroamStream del Memo, te machaca el anterior.

He visto en el foro que a lo mejor un TStream me iría bien pero no tengo nada claro como poder utilizarlo. Lo ideal sería alguna clase que tuviera puntero pra poder seguir incorporando datos fonde me he quedado.

Lo que necesito es alg o así:
Código Delphi [-]
 
var
Hora,Minut:integer;
Begin
Hora:=9;
Minut:=30;
Try
While Minut<41 do begin    
Stream1:=TFileStream.Create('C:\200801220'+IntToStr(Hora)+IntToSTr(Minut)+'_Demanda_Inst.csv',fmOpen  Read);
{   
Poner en algun sitio intermedio esta información, y seguir cargando todos los CSV's
}    
    Minut:=Minut+5;
Finally;
  Stream1.Free;
end;

Muchas gracias de antemano!!:o

Un saludo!

dec 12-02-2008 18:54:53

Hola,

Propongo algo así, aunque, no sé si te servirá o qué. ;)

Código Delphi [-]
function FileToStr(
 path: string): string;
var
  aFile: TextFile;
begin
  AssignFile(aFile, path);
  Reset(aFile);
  while not Eof(aFile) do
    ReadLn(aFile, result);
  CloseFile(aFile);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  s: string;
  sr: TSearchRec;
begin
  SetCurrentDir(ExtractFileDir(ParamStr(0)));
  if FindFirst('.\*.csv', faAnyFile, sr) = 0 then
  begin
    try
      repeat
        s :=  s + FileToStr('.\' + sr.Name)
      until (FindNext(sr) <> 0);
    finally
      FindClose(sr);
    end;
  end;

  ShowMessage(s);
end;

keyboy 12-02-2008 19:08:19

Cita:

Empezado por Albertito
El LoadFromFile o LoadFroamStream del Memo, te machaca el anterior.

Bueno, sí, pero puedes usar un TStringList auxiliar para leer cada archivo (LoadFromFile) y luego usar Memo1.Lines.AddStrings para agregar al memo sin machacar nada.

Bye

dec 12-02-2008 19:09:57

Hola,

Yo sigo a lo mío... :D :D

Código Delphi [-]
function ReadTextFiles(path,
 extension: string): string;

  function FileToStr(
   path: string): string;
  var
    aFile: TextFile;
  begin
    AssignFile(aFile, path);
    Reset(aFile);
    while not Eof(aFile) do
      ReadLn(aFile, result);
    CloseFile(aFile);
  end;

var
  sr: TSearchRec;
begin
  result := EmptyStr;
  path := ExtractFilePath(path);
  if FindFirst(path + extension,
   faAnyFile, sr) = 0 then begin
     try
       repeat
         result := Concat(
           result, FileToStr(path + sr.Name)
         );
       until (FindNext(sr) <> 0);
     finally
       FindClose(sr);
     end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(
    ReadTextFiles('.\', '*.csv')
  );
end;

dec 12-02-2008 19:20:30

Hola,

He descubierto que no estaba leyendo de los archivos sino una línea. ;)

Código Delphi [-]
function ReadTextFiles(
 path, mask: string): string;

  function FileToStr(
   path: string): string;
  var
    line: string;
    aFile: TextFile;
  begin
    AssignFile(aFile, path);
    Reset(aFile);
    while not Eof(aFile) do
    begin
      ReadLn(aFile, line);
      result := result + line;
    end;
    CloseFile(aFile);
  end;

var
  sr: TSearchRec;
begin
  result := EmptyStr;
  path := ExtractFilePath(path);
  if FindFirst(path + mask,
   faAnyFile, sr) = 0 then begin
     try
       repeat
         result := result +
          FileToStr(path + sr.Name);
       until (FindNext(sr) <> 0);
     finally
       FindClose(sr);
     end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Text := ReadTextFiles('.\', '*.csv');
end;

keyboy 12-02-2008 20:07:07

Cita:

Empezado por dec
Yo sigo a lo mío...

Podemos combinar ideas:

Código Delphi [-]
procedure ReadFiles(Path, Mask: String; Lines: TStrings);
var
  SearchRec: TSearchRec;
  SomeLines: TStringList;
  Result: Integer;

begin
  Path := ExtractFilePath(Path);
  Result := FindFirst(Path + Mask, faAnyFile, SearchRec);

  try
    if Result = 0 then
    begin
      SomeLines := TStringList.Create;

      try
        repeat
          SomeLines.LoadFromFile(Path + SearchRec.Name);
          Lines.AddStrings(SomeLines);

          Result := FindNext(SearchRec);
        until Result <> 0;
      finally
        SomeLines.Free;
      end;
    end;
  finally
    FindClose(SearchRec);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ReadFiles('.\', '*.csv', Memo1.Lines);
end;

Bye

dec 12-02-2008 20:27:02

Hola,

Pues claro que podemos combinar ideas, no faltaba más. ;)

Código Delphi [-]

begin
  Result := _CopyObject('.\', '*.csv');
end;

Je, je, je... No he podido evitarlo. ;) :D :)

egostar 12-02-2008 20:44:33

Cita:

Empezado por dec (Mensaje 265344)
Hola,

Pues claro que podemos combinar ideas, no faltaba más. ;)

Código Delphi [-]
 
begin
   Result := _CopyObject('.\', '*.csv');
end;

Je, je, je... No he podido evitarlo. ;) :D :)

Cuidaditoo con esa función, a ver si un día de estos se devela el misterio :D:D:D

Salud OS

Albertito 12-02-2008 22:30:49

Muchas Gracias a todos por vuestra rapidez así como por la clase magistral que me habéis dado. Lo he probado y me va perfecto!!!!! Me habéis sacado un peso de encima!

Gracias de nuevooo!!


La franja horaria es GMT +2. Ahora son las 13:00:09.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi