unit Form.Main;
interface
uses
System.IOUtils, System.Types,
Winapi.Windows,
Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs,
Vcl.StdCtrls;
type
TForm1 =
class( TForm )
ListBox1: TListBox;
Button1: TButton;
PathEdit: TEdit;
SearchTextEdit: TEdit;
procedure Button1Click( Sender: TObject );
private
procedure FindMyFiles;
/// <summary>
/// Lädt den Inhalt aus der DFM Datei in einen String
/// </summary>
function GetDfmContentFromFile(
const AFilename:
string ):
string;
/// <summary>
/// Überarbeitet den DfmContent für die einfachere Suche nach dem Text
/// </summary>
function PrepareDfmContentForSearch(
const ADfmContent:
string ):
string;
/// <summary>
/// Durchsucht den DfmContent nach dem Suchtext
/// </summary>
function DfmContentContainsText(
const ADfmContent, ASearchText:
string ): Boolean;
/// <summary>
/// Erzeugt den Such-Filter
/// </summary>
function BuildDirectoryFilterPredicate( ASearchText:
string ): TDirectory.TFilterPredicate;
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TForm1 }
procedure TForm1.Button1Click( Sender: TObject );
begin
FindMyFiles;
end;
procedure TForm1.FindMyFiles;
var
LFiles: TStringDynArray;
LStrs: TStringList;
LValue:
string;
begin
ListBox1.Clear;
LFiles := TDirectory.GetFiles(
{Path} PathEdit.Text,
{SearchPattern} '
*.dfm',
{SearchOption} TSearchOption.soAllDirectories,
{Predicate} BuildDirectoryFilterPredicate( SearchTextEdit.Text ) );
ListBox1.Items.BeginUpdate;
try
for LValue
in LFiles
do
begin
ListBox1.Items.Add( LValue );
end;
finally
ListBox1.Items.EndUpdate;
end;
end;
function TForm1.BuildDirectoryFilterPredicate( ASearchText:
string ): TDirectory.TFilterPredicate;
begin
Result :=
function(
const Path:
string;
const SearchRec: TSearchRec ): Boolean
begin
Result := DfmContentContainsText( PrepareDfmContentForSearch( GetDfmContentFromFile( TPath.Combine( Path, SearchRec.
Name ) ) ), ASearchText );
end;
end;
function TForm1.GetDfmContentFromFile(
const AFilename:
string ):
string;
begin
Result := TFile.ReadAllText( AFilename );
end;
function TForm1.PrepareDfmContentForSearch(
const ADfmContent:
string ):
string;
begin
Result := ADfmContent;
end;
function TForm1.DfmContentContainsText(
const ADfmContent, ASearchText:
string ): Boolean;
begin
Result := ADfmContent.
Contains( ASearchText );
end;
end.