delphi 实现类似windows的查找功能-遍历整个硬盘目录


本文整理自网络,侵删。

 注:输入c:\** 即可遍历C盘
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
private
    { Private declarations }
public
    { Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure   search(dir:string);   
var   
      targetpath:string;{目标路径名}   
      sr:TsearchRec;   
begin   
      {第一阶段:找出初始dir目录下的所有文件,   
      其中dir变量值由edit1的Text属性确定}  
      targetpath:=extractfilepath(dir);{分解出目标路径名}   
      if   findfirst(dir,faanyfile,sr)=0   then   
      repeat   
        if((sr.name<>'.')and(sr.name<>'..'){排除父目录和本目录两个假文件}   
        and((filegetattr(targetpath+sr.name)and   fadirectory)<>fadirectory)){只取文件}   
        then
            form1.memo1.Lines.Add(targetpath+sr.name);{在memo中添加找到的文件}   
      until   findnext(sr)<>0;   
    
      if   findfirst(dir,faanyfile,sr)=0   then   
      repeat   
        if((sr.name<>'.')and(sr.name<>'..')){排除父目录和本目录两个假文件}   
        and((filegetattr(targetpath+sr.name)and   fadirectory)=fadirectory){排除文件}
        then
            search(targetpath+sr.name+'\*.*');{递归调用}
      until   findnext(sr)<>0;   
end;   
   

procedure TForm1.Button1Click(Sender: TObject);
begin
memo1.Clear;{清除数据表memo字段内容}   
search(Edit1.Text);{调用Search()函数}
MessageDlg('文件搜索完毕!',mtInformation,[mbOk],0);{结束提示}
end;
end.

相关阅读 >>

Delphi 时钟

Delphi 解决strtodatetime()不是有效日期类型的问题

Delphi 防止程序重复打开运行

Delphi动态设置spcomm的属性值

Delphi 使用edge browser浏览器组件

Delphi 控制台 输入输出实例代码

Delphi 跨平台获取文件列表

Delphi中实现磁盘信息类(tdriveinfo)

Delphi 关于字符串的一些使用技巧

Delphi驱动方式winio模拟按键

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...