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
for 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;
only one problem, when the file is large, it hangs, has something that can improve ? The file . TXT has 90KB more has 5 thousand lines...
– user7605
Can you help me in this example if it wouldn’t bother Tiago ? if you were using Readln ? if you can I really appreciate it..
– user7605
Thank you @Tiago Silva.
– user7605
see to help: http://prntscr.com/4t4xax
– user7605
Thanks @Tiago Silva, I’m trying to figure out why it hangs, my file is only 80kb, it’s like I’m giving a LOOP ? small file goes of good.... has some idea friend ?
– user7605
I think I figured it out here, I’ll test if it works out I’m gonna.... really generating a loop...
– user7605