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;