请问如何实现按内容搜索某目录中的文件?
请问如何实现按内容搜索某目录中的文件?并列出这些文件呢
就是按某个关键字,查找某个目录,目录中的文件如果包含这个关键字,就列出该文件名
帮忙提供一下思路,或者如何调用Windows或Google的桌面搜索也可以
[解决办法]
使用Windows Index Service进行全文检索
参考VB的例子:
、管理:
使用“计算机管理”,可以进行索引服务的管理。
索引服务的管理主要包括Catalog(编目)和Scope(目录)
使用脚本管理:
Set objAdminIS = CreateObject("Microsoft.ISAdm")
objAdminIS.Stop
Set objCatalog = objAdminIS.AddCatalog("编目名称", "编目存储地址")
objCatalog.AddScope("要进行编目的目录", "true:进行编目,false:不进行编目(排除该目录)","用户名(可选)","口令(可选)")
objAdminIS.Start
2、进行检索 (VB)
Set objConnection = CreateObject("ADODB.Connection")
objConnection.ConnectionString = "provider=msidxs;"
objConnection.Properties("Data Source") = "system" 'system是IndexService默认建立的编目
objConnection.Open
Set objCommand = CreateObject("ADODB.Command")
strQuery = "Select directory, Path,Filename, Size, Contents from scope() Where Freetext('关键字')"
'SELECT 子句中可以使用的字段可以在 [计算机管理][索引服务][编目][属性]中查到
'WHERE中可以使用 Size>5000 ,AND,OR,Path LIKE 'F:\%'等等
Set objRecordSet = objConnection.Execute(strQuery)
Do While Not objRecordSet.EOF
Debug.Print objRecordSet("directory"), objRecordSet("Filename"), objRecordSet("Size")
objRecordSet.MoveNext
Loop
[解决办法]
我来了!代码如下:
procedure TForm1.Button1Click(Sender: TObject);
var
ADir: string;
sr: TSearchRec;
AFileCount: Integer;{文件个数}
strname:string;{文件名}
begin
ADir := 'c:\Skins\';
AFileCount := 0;
strname:='Steam.asz' ;{也可以是*.asz}
if FindFirst(ADir + strname, 0, sr) = 0 then
begin
repeat
begin
ListBox1.Items.Add(ADir + sr.Name);//文件列表
Inc(AFileCount);
end;
until FindNext(sr) <> 0;
FindClose(sr);
end;
//Label1.Caption := IntToStr(AFileCount);//文件数量
end;
//=============窗体页object Form1: TForm1 Left = 192 Top = 107 Width = 813 Height = 505 Caption = 'Form1' Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'MS Sans Serif' Font.Style = [] OldCreateOrder = False DesignSize = ( 805 478) PixelsPerInch = 96 TextHeight = 13 object ListBox1: TListBox Left = 224 Top = 0 Width = 577 Height = 473 Anchors = [akLeft, akTop, akRight, akBottom] ItemHeight = 13 TabOrder = 0 end object GroupBox5: TGroupBox Left = 0 Top = 0 Width = 217 Height = 69 Caption = '搜索路径:' TabOrder = 1 object cobPath: TComboBox Left = 11 Top = 20 Width = 198 Height = 21 Style = csDropDownList DropDownCount = 20 ItemHeight = 13 ItemIndex = 0 TabOrder = 0 Text = 'C:\' Items.Strings = ( 'C:\') end object Button1: TButton Left = 148 Top = 44 Width = 61 Height = 20 Caption = '浏览...' TabOrder = 1 OnClick = Button1Click end end object GroupBox4: TGroupBox Left = 0 Top = 72 Width = 217 Height = 47 Caption = '搜索类型:' TabOrder = 2 object edtType: TEdit Left = 11 Top = 20 Width = 198 Height = 21 TabOrder = 0 Text = '*.ini' end end object GroupBox3: TGroupBox Left = 0 Top = 120 Width = 217 Height = 47 TabOrder = 3 object CheckBox3: TCheckBox Left = 13 Top = 0 Width = 78 Height = 17 Caption = '搜索内容:' TabOrder = 0 end object edtContent: TEdit Left = 11 Top = 20 Width = 198 Height = 21 TabOrder = 1 end end object GroupBox1: TGroupBox Left = 0 Top = 168 Width = 217 Height = 47 TabOrder = 4 object Label3: TLabel Left = 90 Top = 25 Width = 39 Height = 13 AutoSize = False Caption = 'KB至' Transparent = True end object Label4: TLabel Left = 195 Top = 25 Width = 39 Height = 13 AutoSize = False Caption = 'KB' Transparent = True end object cbSize: TCheckBox Left = 13 Top = 0 Width = 78 Height = 17 Caption = '文件大小:' TabOrder = 0 end object edtSmall: TEdit Left = 11 Top = 20 Width = 58 Height = 21 TabOrder = 1 Text = '0' end object edtLarge: TEdit Left = 118 Top = 20 Width = 58 Height = 21 TabOrder = 2 Text = '0' end object UpDown1: TUpDown Left = 69 Top = 20 Width = 15 Height = 21 Associate = edtSmall Min = 0 Position = 0 TabOrder = 3 Wrap = False end object UpDown2: TUpDown Left = 176 Top = 20 Width = 15 Height = 21 Associate = edtLarge Min = 0 Position = 0 TabOrder = 4 Wrap = False end end object GroupBox2: TGroupBox Left = 0 Top = 216 Width = 217 Height = 43 TabOrder = 5 object Label6: TLabel Left = 104 Top = 20 Width = 39 Height = 13 AutoSize = False Caption = '至' Transparent = True end object cbDate: TCheckBox Left = 13 Top = 0 Width = 78 Height = 17 Caption = '修改日期:' TabOrder = 0 end object dtpBegin: TDateTimePicker Left = 11 Top = 16 Width = 86 Height = 20 CalAlignment = dtaLeft Date = 39314.8033512269 Time = 39314.8033512269 DateFormat = dfShort DateMode = dmComboBox Kind = dtkDate ParseInput = False TabOrder = 1 end object dtpEnd: TDateTimePicker Left = 120 Top = 16 Width = 89 Height = 20 CalAlignment = dtaLeft Date = 39314.8033512269 Time = 39314.8033512269 DateFormat = dfShort DateMode = dmComboBox Kind = dtkDate ParseInput = False TabOrder = 2 end end object btnSearch: TButton Left = 144 Top = 264 Width = 75 Height = 25 Caption = '开始查找' TabOrder = 6 OnClick = btnSearchClick endend