请问如何实现按内容搜索某目录中的文件?并列出这些文件呢
就是按某个关键字,查找某个目录,目录中的文件如果包含这个关键字,就列出该文件名
帮忙提供一下思路,或者如何调用Windows或Google的桌面搜索也可以

解决方案 »

  1.   

    使用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   
      

  2.   

    我来了!代码如下: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;
      

  3.   


    strname:='St*.asz' ;{查有ST字名的目件}
    strname:='S*m.asz' ;
      

  4.   

    怎样对WDS(WINDOWS的桌面搜索)进行开发,WDS是支持开发的
      

  5.   


    //=============窗体页
    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
      end
    end
      

  6.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ComCtrls, ShlObj;type
      TGnSearchFile = Record
        FileName:String;
        BContent:Boolean;
        Content:String;
        BSize:Boolean;
        SmallSize:Integer;
        LargeSize:Integer;
        BDate:Boolean;
        BeginDate:TDateTime;
        EndDate:TDateTime;
      end;  TForm1 = class(TForm)
        ListBox1: TListBox;
        GroupBox5: TGroupBox;
        cobPath: TComboBox;
        GroupBox4: TGroupBox;
        edtType: TEdit;
        GroupBox3: TGroupBox;
        CheckBox3: TCheckBox;
        edtContent: TEdit;
        GroupBox1: TGroupBox;
        Label3: TLabel;
        Label4: TLabel;
        cbSize: TCheckBox;
        edtSmall: TEdit;
        edtLarge: TEdit;
        UpDown1: TUpDown;
        UpDown2: TUpDown;
        GroupBox2: TGroupBox;
        Label6: TLabel;
        cbDate: TCheckBox;
        dtpBegin: TDateTimePicker;
        dtpEnd: TDateTimePicker;
        btnSearch: TButton;
        procedure btnSearchClick(Sender: TObject);
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
        function SearchFile(TheDirectory :String ;var GnFile:TGnSearchFile) : Boolean;
      end;var
      Form1: TForm1;implementation{$R *.dfm}Function SameName(N1,N2:string) : Boolean;
    var
      I:Integer;
    begin
      try
        Result := False;
        if (Length(N1)=0)or(N1='*') then
        begin
          Result := True;
        end
        else if N1[1]='*' then
        begin
          Result := SameName(Copy(N1,2,MaxInt),Copy(N2,Pos(N1[2],N2),MaxInt));
          if not Result then
          begin
            N2 := Copy(N2,Pos(N1[2],N2)+1,MaxInt);
            if Pos(N1[2],N2)>0 then
            begin
              Result := SameName('*'+Copy(N1,2,MaxInt),Copy(N2,Pos(N1[2],N2),MaxInt));
            end;
          end;
        end
        else if (N1[1]='?')or(N1[1]=N2[1])then
        begin
          Result := SameName(Copy(N1,2,MaxInt),Copy(N2,2,MaxInt));
        end
        else
        begin
          Result := False;
        end;
      except
        ;
      end;
    end;function TForm1.SearchFile(TheDirectory :String ;var GnFile:TGnSearchFile) : Boolean;
    var
      SearchRec:TSearchRec;
      Res,I:Integer;
      bRight:Boolean;
      List:TStrings;
    begin
      Result := False;
      try
        if TheDirectory[Length(TheDirectory)]<>'\' then TheDirectory:= TheDirectory+'\';
        Res := FindFirst(TheDirectory + '*.*', faAnyFile, SearchRec);
        while (Res = 0) do
        begin
          if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
          begin
            if ((SearchRec.Attr and faDirectory) > 0) then
            begin
              SearchFile(TheDirectory + SearchRec.Name, GnFile);
            end
            else
            begin
              if SameName(LowerCase(GnFile.FileName),LowerCase(SearchRec.Name)) then
              begin
                bRight := True;
                if GnFile.BSize then
                begin
                  if (SearchRec.Size >GnFile.SmallSize) and (SearchRec.Size < GnFile.LargeSize) then
                  begin
                    bRight := True;
                  end else bRight := False;
                end;
                if GnFile.BDate then
                begin
                  if (SearchRec.Time >DateTimeToFileDate(GnFile.BeginDate)) and (SearchRec.Time < DateTimeToFileDate(GnFile.EndDate+1)) then
                  begin
                    bRight := True;
                  end else bRight := False;
                end;
                if bRight then
                begin
                  if GnFile.BContent then
                  begin
                    List:=TStringList.Create ;
                    List.LoadFromFile(TheDirectory+SearchRec.Name);
                    if Pos(GnFile.Content,List.Text)>1 then
                    begin
                      ListBox1.Items.Add(TheDirectory+SearchRec.Name );
                    end;
                    List.Free;
                  end
                  else ListBox1.Items.Add(TheDirectory+SearchRec.Name );
                  Application.ProcessMessages;
                end;
              end;
            end;
          end;
          Res := FindNext(SearchRec);
        end;
        Result := True;
      finally
        FindClose(SearchRec);
      end;
    end;procedure TForm1.btnSearchClick(Sender: TObject);
    var
      GnFileInfo:TGnSearchFile;
    begin
      ListBox1.Items.Clear;
      GnFileInfo.BContent := (edtContent.Text <>'');
      GnFileInfo.Content   := edtContent.Text;
      GnFileInfo.BSize := cbSize.Checked;
      GnFileInfo.SmallSize := StrToInt(edtSmall.Text) * 1024;
      GnFileInfo.LargeSize := StrToInt(edtLarge.Text) * 1024;
      if GnFileInfo.SmallSize>GnFileInfo.LargeSize then
      begin
        GnFileInfo.LargeSize := MaxInt;
      end;
      GnFileInfo.BDate :=  cbDate.Checked;
      GnFileInfo.BeginDate := dtpBegin.DateTime;
      GnFileInfo.EndDate := dtpEnd.DateTime ;
      GnFileInfo.FileName  := edtType.Text;
      SearchFile(cobPath.Text,GnFileInfo);
    end;function BrowseforFile(Handle : THandle; Title : String; Filename : String) : String;
    var 
      BrowseInfo : TBrowseInfo;
      RetBuffer,
      FName,
      ResultBuffer : Array [0..255] of char;
      PIDL : PItemIDList;
    begin 
      StrPCopy(Fname,FileName); 
      FillChar(BrowseInfo,SizeOf(TBrowseInfo),#0);
      Fillchar(RetBuffer,SizeOf(RetBuffer),#0); 
      FillChar(ResultBuffer,SizeOf(ResultBuffer),#0); 
      BrowseInfo.hwndOwner := Handle;
      BrowseInfo.pszDisplayName := @Retbuffer; 
      BrowseInfo.lpszTitle := @Title[1]; 
      BrowseInfo.ulFlags := BIF_StatusText;
      BrowseInfo.lParam := Integer(@FName);
      PIDL := SHBrowseForFolder(BrowseInfo);
      if SHGetPathFromIDList(PIDL,ResultBuffer) then
      result := StrPas(ResultBuffer) 
      else 
      Result := ''; 
      GlobalFreePtr(PIDL);  
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      strDirectory:String;
    begin
      strDirectory := BrowseforFile(Handle,'选择要搜索文件和目录','');
      if strDirectory<>'' then
      begin
        cobPath.Items.Add(strDirectory);
        cobPath.ItemIndex := cobPath.Items.Count -1;
      end;
    end;end.实现文件名的模糊查找、按日期、大小、内容进行查找
      

  7.   

    谢谢楼上,但是按内容查找对DOC,EXCEL,PPT不行,只适合htm,txt
      

  8.   

    还有没有按针对OFFICE产品内容查找的其他方式?在网上查了很久,没有找到相关资料。
    看来只有集成桌面搜索才能解决问题啊