在delphi6中,如何来实现类似于windows自带的“搜索文件”这项功能呢?并且要得到该文件所在的地址。
例如,我在edit1中输入要查询的文件名是123,要在D:盘下面来查,输入好这两项以后,点击“查询”按钮,如果有D盘有该文件,则该文件的绝对路径显示在label1中,没有查到则弹出反馈信息:对不起,没有你要查找的文件!
还请高手帮忙!

解决方案 »

  1.   

    如果在单个文件夹下搜索文件是否存在可以使用FileExists,返回文件路径可以使用ExtractFilePath.不过要实现类似Windows的搜索功能,我就不知道了,帮你顶顶吧.
    这方面的内容好像有人提出过,你可以在论坛搜索一下...
      

  2.   

    procedure TForm1.Button1Click(Sender: TObject);var
      sr: TSearchRec;
      FileAttrs: Integer;
    begin
      StringGrid1.RowCount := 1;
      if CheckBox1.Checked then
        FileAttrs := faReadOnly
      else
        FileAttrs := 0;
      if CheckBox2.Checked then
        FileAttrs := FileAttrs + faHidden;
      if CheckBox3.Checked then
        FileAttrs := FileAttrs + faSysFile;
      if CheckBox4.Checked then
        FileAttrs := FileAttrs + faVolumeID;
      if CheckBox5.Checked then    FileAttrs := FileAttrs + faDirectory;
      if CheckBox6.Checked then
        FileAttrs := FileAttrs + faArchive;
      if CheckBox7.Checked then    FileAttrs := FileAttrs + faAnyFile;  with StringGrid1 do
      begin
        RowCount := 0;    if FindFirst(Edit1.Text, FileAttrs, sr) = 0 then    begin
          repeat
            if (sr.Attr and FileAttrs) = sr.Attr then
            begin
            RowCount := RowCount + 1;
            Cells[1,RowCount-1] := sr.Name;
            Cells[2,RowCount-1] := IntToStr(sr.Size);
            end;
          until FindNext(sr) <> 0;
          FindClose(sr);
        end;
      end;
    end;
    参见findfirst 和findnext可以满足你的要求
      

  3.   

    hacking(hacking):小人愚笨,我看不懂干吗要放一个StringGrid和7个CheckBox?
    还有就是,我要查询的内容在哪里输入?盘符呢?又在那里输入?望您指点一二!小人不胜感激!
      

  4.   

    to(小人):(hacking)给了你代码 ,我给你点备注吧
    //----开始触发点击搜索按钮事件
    procedure TForm1.Button1Click(Sender: TObject);var
      sr: TSearchRec;//搜索函数 详见delphi自带帮助
      FileAttrs: Integer;//用来保存7个CheckBox的值 这7个CheckBox是为了让你完成类似windows搜索的高级选项的 例如你选择了CheckBox1 那么只搜索只读文件。
    begin
      StringGrid1.RowCount := 1;
      if CheckBox1.Checked then
        FileAttrs := faReadOnly
      else
        FileAttrs := 0;
      if CheckBox2.Checked then
        FileAttrs := FileAttrs + faHidden;
      if CheckBox3.Checked then
        FileAttrs := FileAttrs + faSysFile;
      if CheckBox4.Checked then
        FileAttrs := FileAttrs + faVolumeID;
      if CheckBox5.Checked then    FileAttrs := FileAttrs + faDirectory;
      if CheckBox6.Checked then
        FileAttrs := FileAttrs + faArchive;
      if CheckBox7.Checked then    FileAttrs := FileAttrs + faAnyFile;  with StringGrid1 do
      begin
        RowCount := 0;    if FindFirst(Edit1.Text, FileAttrs, sr) = 0 then//看到edit1了吗 那就是你要填入查询的地方
        begin
          repeat
            if (sr.Attr and FileAttrs) = sr.Attr then
            begin
            RowCount := RowCount + 1;
            Cells[1,RowCount-1] := sr.Name;。//“FindFirst”搜索第一个符合条件的文件
    并显示在StringGrid中
        
            Cells[2,RowCount-1] := IntToStr(sr.Size);
            end;
          until FindNext(sr) <> 0;继续搜索符合条件的文件
    并显示在StringGrid中
        
          FindClose(sr);
        end;
      end;
    end;
    参见findfirst 和findnext可以满足你的要求
    ----------------------------------------------------------
    我也是初学delphi 如有错误 望高手指点
      

  5.   

    谢谢hacking 和 jintian008 ,谢谢你们对我的帮助!谢谢!