Fórum Pesquisa dentro de um arquivo texto #364006

17/09/2008

0

Como eu faço para fazer um pesquisa dentro de um arquivo texto (ascii)? Quero pesquisa por uma determina expresão dentro de arquivo texto, só que e a referida expressão não tem posição certa para estar...

Grato e aguardo qualquer dica.


Aprendiz_ce

Aprendiz_ce

Responder

Posts

19/09/2008

Nasguone

segue um exemplo espero que te ajude
unit UntPrincipal;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
//carrega os delimitadores para a pesquisa
//tudo que estiver entre o delimitador selecionado no caso , sera mostrado no memo
function ListLen(List: string; const Delimiter: string = ´,´): integer;
var DL: integer;
begin
DL := Length(Delimiter);
Result := Ord(List > ´´);
while Pos(Delimiter, List) > 0 do begin
Delete(List, 1, Pos(Delimiter, List)+DL-1);
Inc(Result);
end;
end;
//carrega os delimitadores para a pesquisa
//tudo que estiver entre o delimitador selecionado no caso , sera mostrado no memo
function ListGetAt(List: string; const Position: integer; const Delimiter: string = ´,´): string;
var
i, NP, DL: integer;
begin
NP := 1;
DL := Length(Delimiter);
for i := 1 to Position do begin
List := Copy(List, NP, Length(List)-NP+1);
NP := Pos(Delimiter, List)+DL;
if i = Position then begin
if Pos(Delimiter, List) = 0 then Break;
Delete(List, NP-DL, Length(List)-(NP-DL-1));
end else
if NP = DL then begin
List := ´´;
Break;
end;
end;
Result := List;
end;
//posiciona o delimitador em relação a posição das linhas do arquivo
function ListSetAt(List: string; const Position: integer; const Value: string; const Delimiter: string = ´,´): string;
var
i, NP, DL: integer;
BegStr, EndStr: string;
begin
NP := 1;
DL := Length(Delimiter);
BegStr := ´´;
EndStr := ´´;
for i := 1 to Position do begin
if i > 1 then
BegStr := BegStr+Copy(List, 1, Pos(Delimiter, List)+DL-1);
List := Copy(List, NP, Length(List)-NP+DL);
NP := Pos(Delimiter, List)+DL;
if (NP = DL) and (i < Position) then begin
List := List + Delimiter;
NP := Pos(Delimiter, List)+DL;
end;
if i = Position then begin
if Pos(Delimiter, List) = 0 then Break;
EndStr := Copy(List, NP-DL, Length(List)-(NP-DL-1));
end;
end;
Result := BegStr+Value+EndStr;
end;
// aqui vc coloca os parametros que quer procurar dentro do seu arquivo txt
procedure TForm1.Button1Click(Sender: TObject);
var
Listagem: TStringList;
Pesquisa,teste: string;
i: integer;
j: integer;
begin
Listagem := TStringList.Create;
try
//passa os dados do arquivo txt
Listagem.LoadFromFile(´c:\teste.txt´);
//passa a expressão que vc quer comparar
//observação o parametro deve estar dentro do seu delimitador se vc não quer delimitador
//deixe o mesmo em branco
Pesquisa :=´2 teste´; //aqui estou comparando a segunda linha e a string teste que esta dentro do arquivo teste
//obs 2 pq estou contando do for no arquivo teste não passo o numero apenas a sequencia {,teste,}
for i := 0 to Listagem.Count-1 do
if ListGetAt(Listagem[i], 1) = ´´ then
for j := 2 to ListLen(Listagem[i]) do
if Pesquisa = IntToStr(j)+´ ´+ListGetAt(Listagem[i], j) then
begin
Memo1.Lines.Add(IntToStr(j)+´ ´+ListGetAt(Listagem[i], j));
end;
finally
Listagem.Free;
end;

end;

end.


Responder

Gostei + 0

19/09/2008

Aprendiz_ce

segue um exemplo espero que te ajude unit UntPrincipal; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Memo1: TMemo; Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} //carrega os delimitadores para a pesquisa //tudo que estiver entre o delimitador selecionado no caso , sera mostrado no memo function ListLen(List: string; const Delimiter: string = ´,´): integer; var DL: integer; begin DL := Length(Delimiter); Result := Ord(List > ´´); while Pos(Delimiter, List) > 0 do begin Delete(List, 1, Pos(Delimiter, List)+DL-1); Inc(Result); end; end; //carrega os delimitadores para a pesquisa //tudo que estiver entre o delimitador selecionado no caso , sera mostrado no memo function ListGetAt(List: string; const Position: integer; const Delimiter: string = ´,´): string; var i, NP, DL: integer; begin NP := 1; DL := Length(Delimiter); for i := 1 to Position do begin List := Copy(List, NP, Length(List)-NP+1); NP := Pos(Delimiter, List)+DL; if i = Position then begin if Pos(Delimiter, List) = 0 then Break; Delete(List, NP-DL, Length(List)-(NP-DL-1)); end else if NP = DL then begin List := ´´; Break; end; end; Result := List; end; //posiciona o delimitador em relação a posição das linhas do arquivo function ListSetAt(List: string; const Position: integer; const Value: string; const Delimiter: string = ´,´): string; var i, NP, DL: integer; BegStr, EndStr: string; begin NP := 1; DL := Length(Delimiter); BegStr := ´´; EndStr := ´´; for i := 1 to Position do begin if i > 1 then BegStr := BegStr+Copy(List, 1, Pos(Delimiter, List)+DL-1); List := Copy(List, NP, Length(List)-NP+DL); NP := Pos(Delimiter, List)+DL; if (NP = DL) and (i < Position) then begin List := List + Delimiter; NP := Pos(Delimiter, List)+DL; end; if i = Position then begin if Pos(Delimiter, List) = 0 then Break; EndStr := Copy(List, NP-DL, Length(List)-(NP-DL-1)); end; end; Result := BegStr+Value+EndStr; end; // aqui vc coloca os parametros que quer procurar dentro do seu arquivo txt procedure TForm1.Button1Click(Sender: TObject); var Listagem: TStringList; Pesquisa,teste: string; i: integer; j: integer; begin Listagem := TStringList.Create; try //passa os dados do arquivo txt Listagem.LoadFromFile(´c:\teste.txt´); //passa a expressão que vc quer comparar //observação o parametro deve estar dentro do seu delimitador se vc não quer delimitador //deixe o mesmo em branco Pesquisa :=´2 teste´; //aqui estou comparando a segunda linha e a string teste que esta dentro do arquivo teste //obs 2 pq estou contando do for no arquivo teste não passo o numero apenas a sequencia {,teste,} for i := 0 to Listagem.Count-1 do if ListGetAt(Listagem[i], 1) = ´´ then for j := 2 to ListLen(Listagem[i]) do if Pesquisa = IntToStr(j)+´ ´+ListGetAt(Listagem[i], j) then begin Memo1.Lines.Add(IntToStr(j)+´ ´+ListGetAt(Listagem[i], j)); end; finally Listagem.Free; end; end; end.


Beleza, com certeza vai ajuda sim!

Obrigado pela sua atenção.

Abraço.


Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar