2014-11-04 54 views
0

我想列出窗口使用其Windows Indexing Service編制索引的所有文件。使用Delphi獲取Windows中所有索引文件的列表

指定的文件擴展名是可以接受的。

例如:我正在開發一個軟件,用於呈現用戶媒體,如照片和視頻。我目前使用下面的自定義過程來查找文件自己:

function FindAllFiles_Safe(aDirectory, aFilter: string; aIncludeSubDirs: boolean): string; 
    {$IFDEF DCC} 
    var TD: TDirectory; 
     SO: TSearchOption; 
     DF: TStringDynArray; 
     i: integer; 
     sl: TStringList; 

     MaskArray: TStringDynArray; 
     Predicate: TDirectory.TFilterPredicate; 
     {$ENDIF} 
begin 
    {$IFDEF FPC} 
    result:=FindAllFiles(aDirectory,aFilter,aIncludeSubDirs).text; 
    {$ENDIF} 

    {$IFDEF DCC} 
    MaskArray := SplitString(aFilter, ';'); 


    if aIncludeSubDirs=true then SO:=TSearchOption.soAllDirectories; 

    Predicate := 
    function(const Path: string; const SearchRec: TSearchRec): Boolean 
     var Mask: string; 
    begin 
     for Mask in MaskArray do 
     if MatchesMask(SearchRec.Name, Mask) then 
      exit(True); 
     exit(False); 
    end; 

    //DF:=TD.GetFiles(aDirectory, Predicate, SO); 
    DF:=TD.GetFiles(aDirectory, SO, Predicate); 

    if length(DF)=0 then exit; 


    sl:=TStringList.Create; 
    for i := 0 to length(DF)-1 do sl.Add(DF[i]); 

    result:=sl.Text; 
    sl.Free; 

    {$ENDIF} 
end; 

是否有訪問Windows已經索引文件的方法嗎?
我想利用Windows索引服務來快速檢索文件,而不是浪費資源,如果Windows已經做過之前。

+0

[枚舉Windows中特定類型的文件]的可能重複(http://stackoverflow.com/questions/19272920/enumerating-files-of-specific-type-in​​-windows) – Deltics 2014-11-04 03:48:13

回答

1

There is an API for Windows Search(以前稱爲Windows桌面搜索)。然而,雖然Windows Search API無疑非常強大,但我認爲,僅僅根據文件擴展名(或甚至文件名中的其他組成元素)來定位文件,Windows Search API可能會證明過於複雜並且可以忽略不計除非你處理的是真正非常多的文件。

3

query the index of the Windows Search的其中一種方法是使用ADO和查詢語法(AQS)和SQL。

試試這個示例代碼(關當然,你可以提高SQL語句來過濾和加速的結果)

{$APPTYPE CONSOLE} 

{$R *.res} 

uses 
    ADOInt, 
    SysUtils, 
    ActiveX, 
    ComObj, 
    Variants; 


procedure QuerySystemIndex; 
var 
    Connection : _Connection; 
    RecordSet: _RecordSet; 
    v: Variant; 
begin; 
    OleCheck(CoCreateInstance(CLASS_Connection, nil, CLSCTX_ALL, IID__Connection, Connection)); 
    OleCheck(CoCreateInstance(CLASS_RecordSet, nil, CLSCTX_ALL, IID__RecordSet, RecordSet)); 
    Connection.CursorLocation := adUseClient; 
    Connection.Open('Provider=Search.CollatorDSO;Extended Properties=''Application=Windows'';', '', '', adConnectUnspecified); 
    Recordset.Open('SELECT Top 5 System.ItemPathDisplay FROM SYSTEMINDEX', Connection, adOpenForwardOnly, adLockReadOnly, adCmdText); 
    Recordset.MoveFirst; 
    v:='System.ItemPathDisplay'; 
    while not Recordset.EOF do 
    begin 
     Writeln(Recordset.Fields.Item[v].Value); 
     Recordset.MoveNext(); 
    end; 
end; 


begin 
try 
    CoInitialize(nil); 
    try 
     QuerySystemIndex; 
    finally 
     CoUninitialize; 
    end; 
except 
    on E:EOleException do 
     Writeln(Format('EOleException %s %x', [E.Message,E.ErrorCode])); 
    on E:Exception do 
     Writeln(E.Classname, ':', E.Message); 
end; 
Writeln('Press Enter to exit'); 
Readln; 
end. 

您可以找到替代的方式來訪問該MSDN documentation搜索索引。

相關問題