?? filesearch.pas
字號:
{****************************************************************************
* Project : FileSearch library
* Author : Alexandre GAMBIER
* Date : 24/09/2002
* Unit : FileSearch
* Prefixe :
* Purpose : Search file & folder
****************************************************************************
* Alexandre GAMBIER (24/09/2002) : File creation
* Alexandre GAMBIER (26/09/2002) : Add Date Filter
* Alexandre GAMBIER (27/09/2002) : Add Size Filter
* Alexandre GAMBIER (14/10/2002) : 1/ Replace OnFileFined event
* with OnFileFound
* 2/ Add ExcludeFilters
* 3/ NbPathFounded >> NbPathFound
* 4/ NbFilesFounded >> NbFilesFound
* Alexandre GAMBIER (28/10/2002) : 1/ Add FoundInNbPath in the statistics
* 2/ The LookForDirectory is not override
* by the LookForAnyFile
* 3/ The Filter property is replace by
* the FileNames property which allow to
* select more filters
* 4/ Add the OnAcceptFile event to allow
* user to decide if the must really
* be accept
****************************************************************************}
unit FileSearch;
interface
uses Windows, Classes, Sysutils, Masks;
type
{ Search Options }
TSearchOptions = record
IncludeSubfolder : Boolean; { Search in subfolders }
LookForReadOnlyFile : Boolean; { Search for readonly file }
LookForHiddenFile : Boolean; { Search for hidden file }
LookForSystemFile : Boolean; { Search for system file }
LookForDirectory : Boolean; { Search for folder }
LookForArchiveFile : Boolean; { Search for archive file }
LookForAnyFile : Boolean; { Search for any file }
end;
{ Type of Date filter access }
TDateFilterAccessKind = (dfakCreatedFiles, dfakModifiedFiles, dfakOpenedFiles, dfakAnyFiles);
{ Type of date filter }
TDateFilterKind = (dfkBetween, dfkBefore, dfkAfter, dfkSame);
{ Date Filter }
TDateFilter = record
FilterOnDate : Boolean ; { Activate or not the date filter }
FilterAccessKind : TDateFilterAccessKind; { King of access filter }
DateFilterKind : TDateFilterKind ; { King of date filter }
FirstDate : TDateTime ; { First date }
SecondDate : TDateTime ; { Second date }
end;
{ Type of size filter }
TSizeFilterKind = (sfkSmallerOrEqualTo, sfkBiggerOrEqualTo);
{ Size Filter }
TSizeFilter = record
FilterOnSize : Boolean ; { Activate or not the size filter }
SizeFilterKind : TSizeFilterKind; { Kind of filter }
Size : Integer ; { Size to compare with }
end;
{ Date Informations }
TFileTime = record
CreationTime : SYSTEMTIME; { The time the file was created }
AccessTime : SYSTEMTIME; { The time the file was opened }
WriteTime : SYSTEMTIME; { The time the file was written }
end;
{ File Informations }
TFileInformations = record
Name : String ; { File name }
Path : String ; { PathName }
Attributes : Integer ; { File attirbutes }
Size : Integer ; { Size }
Time : TFileTime; { Times access }
end;
{ Statistics }
TStatistics = record
NbFilesFound : LongWord; { Number of files found matching with serach criteria }
FoundInNbPath : LongWord; { Count the in how many different path the files have been found }
NbPathFound : LongWord; { Number of folders found matching with serach criteria }
TimeNeeded : LongWord; { Time needed by the search operation }
end;
{ Event : OnFileFound > called when a file or a folder is found }
TOnFileFound = procedure (FileFound : TFileInformations) of object;
{ Even : OnChangeFolder > called when the search operation change the path }
TOnChangeFolder = procedure (NewPath : String) of object;
{ Event : OnStatistics > called when search operation is finished }
TOnStatistics = procedure (Stats : TStatistics) of object;
{ Event : OnAcceptFile > called when a file is about to be accept and the user can say yes or no }
TOnAcceptFile = function (FileFound : TFileInformations) : Boolean of object;
{ TFileSearch class }
TFileSearch = class(TObject)
private
protected
{ FileInformation functions }
function GetFileInformations(Path : String; SearchInfos : TSearchRec) : TFileInformations;
{ Filter on attribute }
function GetAttributeFilter : Integer;
function IsAttributesOk(AttrToCheck : Integer) : Boolean;
{ Filter on date }
function IsDateOk(FileInfos : TFileInformations) : Boolean;
{ Filter on Size }
function IsSizeOk(FileInfos : TFileInformations; SizeFilter : TSizeFilter) : Boolean;
{ Include filter }
function IsFileMatching(FileName : String) : Boolean;
function IsAllFileMarkerPresents : Boolean;
{ Exclude Filter }
function ExcludeFile(FileInfos : TFileInformations; Exclude : TStringList) : Boolean;
public
{ Search criteria }
SearchOptions : TSearchOptions;
DateOptions : TDateFilter ;
SizeOptions : TSizeFilter ;
FileNames : TStringList ;
RootPath : String ;
{ Exclude filter }
ExcludeFilters : TStringList;
{ Events }
OnFileFound : TOnFileFound ;
OnChangeFolder : TOnChangeFolder;
OnStatistics : TOnStatistics ;
OnAcceptFile : TOnAcceptFile ;
{ Constructor & Destructor }
constructor Create;
destructor Destroy; override;
{ start search operation }
function Search : Boolean;
end;
implementation
{*****************************************************************************
* Procedure : TFileSearch.Create
* Purpose : Constructor
* Arguments : NONE
* Result : NONE
*****************************************************************************
* Alexandre GAMBIER (24/09/2002) : Creation
*****************************************************************************}
constructor TFileSearch.Create;
begin
inherited Create;
{ Initialize search options }
SearchOptions.IncludeSubfolder := False;
SearchOptions.LookForReadOnlyFile := True ;
SearchOptions.LookForHiddenFile := True ;
SearchOptions.LookForSystemFile := True ;
SearchOptions.LookForDirectory := True ;
SearchOptions.LookForArchiveFile := True ;
SearchOptions.LookForAnyFile := True ;
{ Initialize date filter }
DateOptions.FilterOnDate := False ;
DateOptions.FilterAccessKind := dfakAnyFiles;
DateOptions.DateFilterKind := dfkBefore ;
DateOptions.FirstDate := Date ;
DateOptions.SecondDate := Date ;
{ Initialize size filter }
SizeOptions.FilterOnSize := False ;
SizeOptions.SizeFilterKind := sfkSmallerOrEqualTo;
SizeOptions.Size := 0 ;
{ Initialize exclude filter }
ExcludeFilters := TStringList.Create;
{ Filter & RootPath }
FileNames := TStringList.Create;
FileNames.Add('*.*');
RootPath := '.\' ;
{ Events }
OnFileFound := nil;
OnChangeFolder := nil;
OnStatistics := nil;
end;
{*****************************************************************************
* Procedure : TFileSearch.Destroy
* Purpose : Destructor
* Arguments : NONE
* Result : NONE
*****************************************************************************
* Alexandre GAMBIER (24/09/2002) : Creation
*****************************************************************************}
destructor TFileSearch.Destroy;
begin
{ free exclude filter }
FileNames.Free;
ExcludeFilters.Free;
inherited Destroy;
end;
{*****************************************************************************
* Procedure : TFileSearch.GetAttributeFilter
* Purpose : Initialize the attribute filter
* Arguments : NONE
* Result : Return the attribute filter
*****************************************************************************
* Alexandre GAMBIER (24/09/2002) : Creation
*****************************************************************************}
function TFileSearch.GetAttributeFilter : Integer;
begin
Result := 0;
if SearchOptions.LookForReadOnlyFile=True then Result := Result or faReadOnly ;
if SearchOptions.LookForHiddenFile =True then Result := Result or faHidden ;
if SearchOptions.LookForSystemFile =True then Result := Result or faSysFile ;
if SearchOptions.LookForDirectory =True then Result := Result or faDirectory;
if SearchOptions.LookForArchiveFile =True then Result := Result or faArchive ;
end;
{*****************************************************************************
* Procedure : TFileSearch.GetFileInformations
* Purpose : Convert file information into TFileInformations
* Arguments : Path > pathname of the file
* SearchInfos > informations given by FindFirst & FindNext
* Result : Return TFileInformation about the the file
*****************************************************************************
* Alexandre GAMBIER (24/09/2002) : Creation
*****************************************************************************}
function TFileSearch.GetFileInformations(Path : String; SearchInfos : TSearchRec) : TFileInformations;
var
TmpSysTime : SYSTEMTIME;
TmpFileTime : FILETIME;
begin
Result.Name := SearchInfos.Name;
Result.Path := Path ;
Result.Attributes := SearchInfos.Attr;
Result.Size := SearchInfos.Size;
{ Convert time access }
FileTimeToLocalFileTime(SearchInfos.FindData.ftCreationTime, TmpFileTime);
FileTimeToSystemTime(TmpFileTime, TmpSysTime);
Result.Time.CreationTime := TmpSysTime;
FileTimeToLocalFileTime(SearchInfos.FindData.ftLastAccessTime, TmpFileTime);
FileTimeToSystemTime(TmpFileTime, TmpSysTime);
Result.Time.AccessTime := TmpSysTime;
FileTimeToLocalFileTime(SearchInfos.FindData.ftLastWriteTime, TmpFileTime);
FileTimeToSystemTime(TmpFileTime, TmpSysTime);
Result.Time.WriteTime := TmpSysTime;
end;
{*****************************************************************************
* Procedure : TFileSearch.IsAttributesOk
* Purpose : Check if attributes are equals
* Arguments : AttrToCheck > Attribute 1
* Result : True if ok
*****************************************************************************
* Alexandre GAMBIER (24/09/2002) : Creation
*****************************************************************************}
function TFileSearch.IsAttributesOk(AttrToCheck : Integer) : Boolean;
var
AttrFilter : Integer;
begin
Result := False;
AttrFilter := GetAttributeFilter;
if ((AttrToCheck and faReadOnly ) and (AttrFilter and faReadOnly ))<>0 then Result := True;
if ((AttrToCheck and faHidden ) and (AttrFilter and faHidden ))<>0 then Result := True;
if ((AttrToCheck and faSysFile ) and (AttrFilter and faSysFile ))<>0 then Result := True;
if ((AttrToCheck and faDirectory) and (AttrFilter and faDirectory))<>0 then Result := True;
if ((AttrToCheck and faArchive ) and (AttrFilter and faArchive ))<>0 then Result := True;
end;
{*****************************************************************************
* Procedure : TFileSearch.IsDateOk
* Purpose : Check (if date filter is activate) if the file match with
* the filter
* Arguments : FileInfos > File Informations
* Result : Return True if the file match
*****************************************************************************
* Alexandre GAMBIER (26/09/2002) : Creation
*****************************************************************************}
function TFileSearch.IsDateOk(FileInfos : TFileInformations) : Boolean;
var
CreationTime : TDateTime;
AccessTime : TDateTime;
WriteTime : TDateTime;
CreationOk : Boolean ;
AccessOk : Boolean ;
WriteOk : Boolean ;
DateFilterOk : Boolean ;
FileInfosRef : TFileInformations;
SysTime : SYSTEMTIME;
begin
{ Check if filter on Date is activate }
if DateOptions.FilterOnDate=False then DateFilterOk := True else
begin
{ Initialize variable}
DateFilterOk := False;
CreationOk := False;
AccessOk := False;
WriteOk := False;
FileInfosRef := FileInfos;
{ Set time to 00:00:00 }
{ First Date}
DateTimeToSystemTime(DateOptions.FirstDate, SysTime);
SysTime.wHour := 0;
SysTime.wMinute := 0;
SysTime.wSecond := 0;
SysTime.wMilliseconds := 0;
DateOptions.FirstDate := SystemTimeToDateTime(SysTime);
{ Second Date}
DateTimeToSystemTime(DateOptions.SecondDate, SysTime);
SysTime.wHour := 0;
SysTime.wMinute := 0;
SysTime.wSecond := 0;
SysTime.wMilliseconds := 0;
DateOptions.SecondDate := SystemTimeToDateTime(SysTime);
{ Creation Date }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -