...
type
{ TListFileSearcher }
TListFileSearcher = class(TFileSearcher)
private
FList: TStrings;
protected
procedure DoFileFound; override;
public
constructor Create(AList: TStrings);
end;
...
// This will compute the hash of each file and then input the result to the SQLite DB
procedure TForm1.btnHashSingleFileClick(FullName, Filename, Filepath : string);
var
hashval : string;
begin
SQLite3Connection1.Close;
//SQLite3Connection1.Password := txtPass.Text; // The current password
try
SQLite3Connection1.Open;
SQLTransaction1.Active := True;
hashval := MD5Print(MD5File(FullName));
// Insert the values into the database
// We're using ParamByName which prevents SQL Injection
// http://wiki.freepascal.org/Working_With_TSQLQuery#Parameters_in_TSQLQuery.SQL
SQLQuery1.SQL.Text := 'Insert into TBL_FILES (Filename, Path, Hash) values (:Filename,:Path,:Hash)';
SQLQuery1.Params.ParamByName('Filename').AsString := Filename;
SQLQuery1.Params.ParamByName('Path').AsString := FilePath;
SQLQuery1.Params.ParamByName('Hash').AsString := hashval;
SQLQuery1.ExecSQL;
SQLTransaction1.Commit;
except
ShowMessage('Failed');
end;
end;
// This will find all files in a folder, and then call btnHashSingleFileClick for each file it finds
procedure TForm1.btnHashManyFilesClick(Sender: TObject);
var
TotalFilesToExamine : TStringList;
i : integer;
Filename, FilePath, FullName : string;
TmStarted, TmEnded : TDateTime;
begin
i := 0;
TotalFilesToExamine := TStringList.Create;
if SelectDirectoryDialog1.Execute then
begin
TmStarted := Now;
lblStarted.Caption:= FormatDateTime('HH:MM:SS', Now);
TotalFilesToExamine := FindAllFilesEx(SelectDirectoryDialog1.Filename, '*', True, True);
for i := 0 to TotalFilesToExamine.Count -1 do
begin
FullName := TotalFilesToExamine.Strings[i];
FileName := ExtractFileName(TotalFilesToExamine.Strings[i]);
FilePath := ExtractFilePath(TotalFilesToExamine.Strings[i]);
btnHashSingleFileClick(FullName, FileName, FilePath);
end;
TotalFilesToExamine.Free;
end;
// Now let's update the grid to show the new values to the user:
UpdateGridFILES(nil); // Now update and display the DBGrid with all the SQLite data in it
TmEnded := Now;
lblEnded.Caption:= FormatDateTime('HH:MM:SS', Now);
lblTimeTaken.Caption:= FormatDateTime('HH:MM:SS', TmEnded-TmStarted); // Reports 1m 10s for 500+ regular files!!
end;
procedure TForm1.UpdateGridFILES(Sender: TObject);
begin
SQLite3Connection1.Close;
// Try to perform query
try
SQLite3Connection1.Connected := True;
SQLQuery1.SQL.Clear;
SQLQuery1.SQL.Text := 'Select * from TBL_FILES';
SQLQuery1.Open;
// Allow the DBGrid to view the results of our query
DataSource1.DataSet := SQLQuery1;
DBGrid1.DataSource := DataSource1;
DBGrid1.AutoFillColumns := true;
except
ShowMessage('Unable to query the database');
end;
{ TListFileSearcher }
procedure TListFileSearcher.DoFileFound;
begin
FList.Add(FileName);
end;
constructor TListFileSearcher.Create(AList: TStrings);
begin
inherited Create;
FList := AList;
end;
function TForm1.FindAllFilesEx(const SearchPath: string; SearchMask: string;
SearchSubDirs: boolean; IncludeHiddenDirs: boolean): TStringList;
var
Searcher: TListFileSearcher;
begin
Result := TStringList.Create;
Searcher := TListFileSearcher.Create(Result);
Searcher.DirectoryAttribute := Searcher.DirectoryAttribute or faHidden;
try
Searcher.Search(SearchPath, SearchMask, SearchSubDirs);
finally
Searcher.Free;
end;
end;
end;