⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filesearch.pas

📁 CreateFile Hook with Delphi with AdvHooKLib
💻 PAS
📖 第 1 页 / 共 2 页
字号:
    FileInfosRef.Time.CreationTime.wHour         := 0;
    FileInfosRef.Time.CreationTime.wMinute       := 0;
    FileInfosRef.Time.CreationTime.wSecond       := 0;
    FileInfosRef.Time.CreationTime.wMilliseconds := 0;
    { Access Date }
    FileInfosRef.Time.AccessTime.wHour         := 0;
    FileInfosRef.Time.AccessTime.wMinute       := 0;
    FileInfosRef.Time.AccessTime.wSecond       := 0;
    FileInfosRef.Time.AccessTime.wMilliseconds := 0;
    { Write Date }
    FileInfosRef.Time.WriteTime.wHour         := 0;
    FileInfosRef.Time.WriteTime.wMinute       := 0;
    FileInfosRef.Time.WriteTime.wSecond       := 0;
    FileInfosRef.Time.WriteTime.wMilliseconds := 0;

    { Convert Time  format }
    CreationTime := SystemTimeToDateTime(FileInfosRef.Time.CreationTime);
    AccessTime   := SystemTimeToDateTime(FileInfosRef.Time.AccessTime  );
    WriteTime    := SystemTimeToDateTime(FileInfosRef.Time.WriteTime   );

    { switch DateFilter }
    case DateOptions.DateFilterKind of
      { Check between 2 date}
      dfkBetween:
      begin
        if (CreationTime>=DateOptions.FirstDate) and (CreationTime<=DateOptions.SecondDate) then CreationOk := True;
        if (AccessTime  >=DateOptions.FirstDate) and (AccessTime  <=DateOptions.SecondDate) then AccessOk   := True;
        if (WriteTime   >=DateOptions.FirstDate) and (WriteTime   <=DateOptions.SecondDate) then WriteOk    := True;
      end;
      { Check before }
      dfkBefore:
      begin
        if CreationTime<DateOptions.FirstDate then CreationOk := True;
        if AccessTime  <DateOptions.FirstDate then AccessOk   := True;
        if WriteTime   <DateOptions.FirstDate then WriteOk    := True;
      end;
      { Check after }
      dfkAfter  :
      begin
        if CreationTime>DateOptions.FirstDate then CreationOk := True;
        if AccessTime  >DateOptions.FirstDate then AccessOk   := True;
        if WriteTime   >DateOptions.FirstDate then WriteOk    := True;
      end;
      { Check the same }
      dfkSame:
      begin
        if CreationTime=DateOptions.FirstDate then CreationOk := True;
        if AccessTime  =DateOptions.FirstDate then AccessOk   := True;
        if WriteTime   =DateOptions.FirstDate then WriteOk    := True;
      end;
    end;

    { switch filter access king }
    case DateOptions.FilterAccessKind of
      { On Creation Time }
      dfakCreatedFiles : if CreationOk=True then DateFilterOk := True;
      { On Write Time }
      dfakModifiedFiles: if WriteOk=True then DateFilterOk := True;
      { On Access Time }
      dfakOpenedFiles  : if AccessOk=True then DateFilterOk := True;
      { On any file }
      dfakAnyFiles     : if (CreationOk=True) or (AccessOk=True) or (WriteOk=True) then DateFilterOk := True;
    end;
  end;

  Result := DateFilterOk;

end;

{*****************************************************************************
 * Procedure : TFileSearch.IsSizeOk
 * Purpose   : Check (if size filter is activate) if the file match with
 *             the filter
 * Arguments : FileInfos  > File informations
 *             SizeFilter > Size Filter options
 * Result    : Return true if the file match
 *****************************************************************************
 * Alexandre GAMBIER (27/09/2002) : Creation
 *****************************************************************************}
function TFileSearch.IsSizeOk(FileInfos : TFileInformations; SizeFilter : TSizeFilter) : Boolean;
var
  SizeFilterOk : Boolean;
begin
  { Check if filter on Size is activate }
  if SizeFilter.FilterOnSize=False then SizeFilterOk := True else
  begin
    SizeFilterOk := False;

    case SizeFilter.SizeFilterKind of
      { Smaller or equal to }
      sfkSmallerOrEqualTo: if FileInfos.Size<=SizeFilter.Size then SizeFilterOk := True;
      { Bigger or equal to }
      sfkBiggerOrEqualTo : if FileInfos.Size>=SizeFilter.Size then SizeFilterOk := True;
    end;
  end;

  Result := SizeFilterOk;
end;

{*****************************************************************************
 * Procedure : TFileSearch.IsFileMatching
 * Purpose   : Check if the filename matches whith the filenames filter
 * Arguments : FileName       > Filename to match
 * Result    : Returns True if matching, False if not
 *****************************************************************************
 * Alexandre GAMBIER (28/10/2002) : Creation
 *****************************************************************************}
function TFileSearch.IsFileMatching(FileName : String) : Boolean;
var
  VerifMask    : TMask;
  FileMatching : Boolean;
  Pos          : Integer;
begin
  FileMatching := False;
  Pos          := 0;

  while (FileMatching=False) and (Pos<FileNames.Count) do
  begin
    { Initialize }
    VerifMask := TMask.Create(FileNames.Strings[Pos]);
    { Check}
    FileMatching := VerifMask.Matches(FileName);
    { Free }
    VerifMask.Free;
    { Next FileName }
    Inc(Pos);
  end;

  Result := FileMatching;
end;

{*****************************************************************************
 * Procedure : TFileSearch.IsAllFileMarkerPresents
 * Purpose   : Check if the marker *.* is present in the Filenames
 * Arguments : FileNamesMatch > filenames to check
 * Result    :
 *****************************************************************************
 * Alexandre GAMBIER (28/10/2002) : Creation
 *****************************************************************************}
function TFileSearch.IsAllFileMarkerPresents : Boolean;
var
  Index : Integer;
begin
  Result := FileNames.Find('*.*', Index);
end;

{*****************************************************************************
 * Procedure : ExcludeFile
 * Purpose   : Check if the file must be exclude
 * Arguments : FileInfos  > File informations
 *             Exclude    > exclude filter
 * Result    : True if the file must be exclude
 *****************************************************************************
 * Alexandre GAMBIER (14/10/2002) : Creation
 *****************************************************************************}
function TFileSearch.ExcludeFile(FileInfos : TFileInformations; Exclude : TStringList) : Boolean;
var
  VerifMask   : TMask;
  PosFilter   : Integer;
  MustExclude : Boolean;
begin
  { Init }
  MustExclude := False;
  PosFilter   := 0;

  { Check each exclude filter }
  while (MustExclude=False) and (PosFilter<Exclude.Count) do
  begin
    { Initialize }
    VerifMask := TMask.Create(Exclude.Strings[PosFilter]);
    { Check}
    MustExclude := VerifMask.Matches(FileInfos.Name);
    { Free }
    VerifMask.Free;
    { Next filter }
    Inc(PosFilter);
  end;

  Result := MustExclude;
end;

{*****************************************************************************
 * Procedure : TFileSearch.Search
 * Purpose   : Search files & folder using Search criteria
 * Arguments : NONE
 * Result    : True si ok
 *****************************************************************************
 * Alexandre GAMBIER (24/09/2002) : Creation
 * Alexandre GAMBIER (14/10/2002) : Add exclude filters
 * Alexandre GAMBIER (28/10/2002) : 1/ Add FoundInNbPath in the statistics
 *****************************************************************************}
function TFileSearch.Search : Boolean;
var
  bAcceptFile : Boolean;
  bCont       : Boolean;
  bNewPath    : Boolean;
  bStop       : Boolean;
  bFirstPass  : Boolean;
  ListePath   : TStringList;
  sPathName   : String;
  sFiltreOk   : String;
  stFindData  : TSearchRec;
  FileInfos   : TFileInformations;
  Stats       : TStatistics;
  StartTime   : LongWord;
  IncludeFile : Boolean;

Begin
  Result := true;

  { Initialize variables }
  ListePath    := TStringList.Create;
  bCont        := True;
  bFirstPass   := True;
  sPathName    := RootPath;
  { Initialize statistics }
  Stats.NbFilesFound   := 0;
  Stats.FoundInNbPath  := 0;
  Stats.NbPathFound    := 0;
  Stats.TimeNeeded     := 0;

  StartTime := GetTickCount();

  { Search... }
  while bCont=True do
  begin
    bNewPath := True;
    { Search in folder found previous pass }
    if bFirstPass=False then
    begin
      if ListePath.Count=0 then bCont := False
      else
      begin
        { Get PathName }
        sPathName := ListePath.Strings[0];
        ListePath.Delete(0);
      end;
    end;

    if bCont=True then
    begin
      { Alert about folder change }
      if assigned(OnChangeFolder)=True then OnChangeFolder(sPathName);

      { Initialize filter mask }
      if sPathName[Length(sPathName)]<>'\' then sPathName := sPathName + '\';
      sFiltreOk := sPathName + '*.*';

      { Start search operation for current folder }
      if FindFirst(sFiltreOk, faAnyFile, stFindData)=0 then
      begin
        bStop := False;
        { Look for each files & folder }
        while bStop=False and bCont=True do
        begin
          if (stFindData.Name<>'.') and (stFindData.Name<>'..') then
          begin
            { Save folder name for next pass - if options is set }
            if ((stFindData.Attr and faDirectory)<>0) and (SearchOptions.IncludeSubfolder=True) then ListePath.Add(sPathName+stFindData.Name);

            { Initialize file informations }
            FileInfos := GetFileInformations(sPathName, stFindData);

            { Include File ? }
            IncludeFile := False;
            if IsAttributesOk(stFindData.Attr)=True then IncludeFile := True;
            if (SearchOptions.LookForAnyFile=True) and ((stFindData.Attr and faDirectory)=0) then IncludeFile := True;
            { Verify Date filter }
            if IsDateOk(FileInfos)=False then IncludeFile := False;
            { Verify Size filter }
            if IsSizeOk(FileInfos, SizeOptions)=False then IncludeFile := False;
            { Exclude File ? }
            if ExcludeFile(FileInfos, ExcludeFilters)=True then IncludeFile := False;
            { Don't include file }
            if (stFindData.Attr and faDirectory)<>0 then
            begin
              { folder Check }
              if (IsFileMatching(stFindData.Name)=False) and (IsAllFileMarkerPresents=False) then IncludeFile := False;
            end
            else if IsFileMatching(stFindData.Name)=False then IncludeFile := False; { FileCheck}

            if IncludeFile=True then
            begin
              { User says if the file is accepted }
              bAcceptFile := True;
              if Assigned(OnAcceptFile)=True then bAcceptFile := OnAcceptFile(FileInfos);

              if bAcceptFile=True then
              begin
                if (stFindData.Attr and faDirectory)<>0 then
                begin { folder }
                  Inc(Stats.NbPathFound);
                  { Event }
                  if assigned(OnFileFound)=True then OnFileFound(FileInfos);
                end
                else
                begin { File }
                  { Stats}
                  Inc(Stats.NbFilesFound);
                  if bNewPath=True then
                  begin
                    { If in new folder Increment the number of different folder }
                    bNewPath := False;
                    Inc(Stats.FoundInNbPath);
                  end;
                  { Event }
                  if assigned(OnFileFound)=True then OnFileFound(FileInfos);
                end;
              end;

            end;
          end;

          { next file or folder }
          if FindNext(stFindData)<>0 then bStop := True;
        end;

        { close the current search operation }
        FindClose(stFindData);
      end
      else
      begin
       bCont  := False;
       Result := False;
      end;

      bFirstPass := False;
    end;
  end;

  Stats.TimeNeeded  := GetTickCount()-StartTime;
  { send statistics }
  if assigned(OnStatistics)=True then OnStatistics(Stats);

  { free memory }
  ListePath.Free;
end;

End.




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -