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 安装apk

Delphi vclzip压缩文件夹

Delphi string内存结构

Delphi 间隔时间

Delphi 模拟网站验证码

Delphi 内存管理[4]

Delphi 判断上午还是下午

Delphi getprocessmemoryinfo获取进程占用内存大小

Delphi 掌控pagecontrol中的右上方的左右箭头事件

Delphi 判断系统服务是否运行

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



打赏

取消

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

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

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

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

评论

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