Filter a file

1

See if someone can save me, I have a program where a TXT is generated every day as follows:

  

(16BA = 1cc) (83 = customer1 @ planting) (80 = 0) (82 = 1) (1691 = 610)

I need to create another simple program, which removes from this list only the text that is as follows: nomedocliente@empresa , in case of this example above, I would have to remove the following data and save in a txt or even put in a MEMO:

  

client1 @ plantao, client @ frigorifico

    
asked by anonymous 04.10.2014 / 14:20

2 answers

1

Here's an example of how it could be done.

Using TStringList :

var
  _file: TStringList; // StringList para carregar o arquivo
  text, textDest, aux: string;
  posIni, posFim: integer;
begin
  _file := TStringList.Create;
  try
    // carrega o arquivo
    _file.LoadFromFile('NOME_DO_ARQUIVO');
    text := _file.Text;

    // para interagir linha a linha do arquivo
    while Pos('@', text) > 0 do
    begin
      posIni := Pos('=', text);
      posFim := Pos(')', text) - 1;
      aux := Copy(text, posIni+1, posFim-posIni);
      if Pos('@', aux) > 0 then
      begin
        if Trim(textDest) = '' then
          textDest := aux
        else
          textDest := textDest + ',' + aux;
      end;
      Delete(text, 1, posFim + 1);
    end;

    if Trim(textDest) <> '' then
    begin
      _file.Clear;
      _file.Text := textDest;
      _file.SaveToFile('NOVO_ARQUIVO_NOVO');
    end
    else
    begin
      ShowMessage('Não existem registros!');
    end;

  finally
    _file.Free;
  end;
end;

Using TextFile to get better performance

var
  _file: TextFile;
  _fileDest: TStringList;
  text, textDest, aux: string;
  posIni, posFim: integer;
begin
  AssignFile(_file, 'orig.txt');
  Reset(_file);
  try
    while not Eof(_file) do
    begin
      ReadLn(_file, text);
      while Pos('@', text) > 0 do
      begin
        posIni := Pos('=', text);
        posFim := Pos(')', text) - 1;
        aux := Copy(text, posIni+1, posFim-posIni);
        if Pos('@', aux) > 0 then
        begin
          if Trim(textDest) = '' then
            textDest := aux
          else
            textDest := textDest + ',' + aux;
        end;
        Delete(text, 1, posFim + 1);
      end;
    end;

    if Trim(textDest) <> '' then
    begin
      _fileDest := TStringList.Create;
      try
        _fileDest.Text := textDest;
        _fileDest.SaveToFile('dest.txt');
      finally
        _fileDest.Free;
      end;
    end
    else
    begin
      ShowMessage('Não existem registros!');
    end;
  finally
    CloseFile(_file);
  end;
end;
    
04.10.2014 / 15:09
1

Contributing to the answer I would use regular expressions to solve your problem, it is simpler and more efficient.

A 10000 file was opened and processed in 2 seconds.

See the code.

var
  Texto: string;
  StringList: TStringList;
begin         
    StringList:= TStringList.Create;
    try
      StringList.LoadFromFile( 'c:\aquivo.txt' );

      Texto:= TRegEx.Replace( StringList.Text, '[A-Za-z0-9]+@[A-Za-z0-9]+', '' );

      Memo1.Clear;
      Memo1.Text:= Texto;       
    finally
      FreeAndNil(StringList);
    end;    
  end;

The full project is at: link

Article talking about regular expressions: link

    
06.10.2014 / 14:15