Cita:
Empezado por Paulao
ecfisa, hizo algunas alteraciones y ahora estas. Una pregunta: Como hago para hacer busqueda en .pas y .dfm al mismo tiempo con FindFirst?
|
No se puede hacer con FindFirst en una sola llamada, sin embargo se me ocurre un truco usando
TStrings. Podría implementarse de forma recursiva´pero consume más recursos y quizá
sea un poco más lento.
Código Delphi
[-]
function TextFoundInFile(Ruta: string; const FileMask:string; const Buscado: string): TStrings;
var
SR: TSearchRec;
txt: TextFile;
Row: string;
Found: Boolean;
NroExt: TStrings;
i: Integer;
begin
NroExt:= TStringList.Create;
try
NroExt.Delimiter:= ';';
NroExt.DelimitedText:= FileMask;
Ruta:= IncludeTrailingPathDelimiter(Ruta);
Result:= TStringList.Create;
for i:= 0 to NroExt.Count - 1 do
begin
if FindFirst(Ruta + NroExt[i], faAnyFile - faDirectory, SR) = 0 then
repeat
AssignFile(txt,Ruta + SR.Name);
Reset(txt);
Found:= False;
while not Eof(txt) and not Found do
begin
Readln(txt, Row);
if Pos(Buscado, Row) > 0 then
begin
Result.Add(Ruta + SR.Name);
Found:= True
end
end;
CloseFile(txt);
until FindNext(SR) <> 0
end
finally
NroExt.Free
end
end;
Llamada:
Código Delphi
[-]
procedure TForm1.Button1Click(Sender: TObject);
begin
edFileMask.Text:= '*.PAS; *.DFM'; ListBox1.Items:= TextFoundInFile(edRuta.Text, edFileMask.Text, edBuscado.Text).SaveToFile('C:\LISTA.TXT')
end;
De ese modo podés pasar la máscara que desees al argumento
FileMask, por ejemplo: '*.PAS;*.DFM;*.TXT;*.BAT;*.RC'
Saludos.