delphi 全盘搜索指定文件


本文整理自网络,侵删。

 方法一:(特点是速度极快160G硬盘全盘搜索只要十几秒)


======================================================================

procedure TForm1.GetFile(PathName: string); stdcall;
var
FindData: TWin32FindData;
hf:THandle;
b:boolean;
TmpStr,TempFolder,Str:string;

begin
hf := Windows.FindFirstFile(PChar(PathName + '\*.*'), FindData);
if hf = INVALID_HANDLE_VALUE then exit;
b := true;
while b do
begin
if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0 then
begin
Str:=string(FindData.cFileName);
if Str='cmd.exe' then//查看文件名中是否有cmd.exe
begin
ListBox1.Items.Add(PathName+'\'+Str );
MessageBox(0,pchar(PathName+'\'+Str),'找到了',MB_OK);
Edit1.Text:=PathName+'\'+Str;
end;
end
else
begin
TmpStr := FindData.cFileName + '';
if (TmpStr <> '.') and (TmpStr <> '..') then
begin
TempFolder:=TempFolder+string(FindData.cFileName)+'\';
GetFile(PathName+'\'+ FindData.cFileName);
Label2.Caption:=PathName+'\'+ FindData.cFileName; //显示当前查找进度
Application.ProcessMessages; //实时刷新
end;
end;
b:=Windows.FindNextFile(hf,FindData);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
I: Integer;
Drive: PChar;
//tid:longword;
begin
for I := 0 to 31 do
begin
if Boolean(GetLogicalDrives and (1 SHL I)) then
begin
Drive:= PChar(CHR(65 + I) + ':\');
ListBox1.Items.Add( '正在查找'+Drive+'盘文件...' );
// CreateThread(nil,0,@TForm1.GetFile,pchar('c:\'),0,tid);
GetFile(Drive);
ListBox1.Items.Add( Drive+'盘文件查找完毕!' );
end;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
WinExec(Pchar(Edit1.text),SW_SHOW);
end;

end.


================================================================

方法二:

function EnumFileInRecursion(path:PChar):Longint;stdcall;
var
searchRec:TSearchRec;
found:Integer;
tmpStr:String;
begin
Result:=0;
tmpStr:=StrPas(path)+'\*.*';
found:=FindFirst(tmpStr,faAnyFile,searchRec);
while found=0 do
begin
if (searchRec.Attr and faDirectory)<>0 then
begin
if (searchRec.Name <> '.') and (searchRec.Name <> '..') then
begin
tmpStr:=StrPas(path)+'\'+searchRec.Name;
Result:=Result+EnumFileInRecursion(PChar(tmpStr));
end;
end
else
begin
Result:=Result+1;
if searchRec.Name='cmd.exe' then
begin
ShowMessage('找到文件 '+StrPas(path)+'\'+searchRec.Name);
FindClose(searchRec); //找到指定文件后停止遍历
end;
Form1.Memo1.Lines.Add(StrPas(path)+'\'+searchRec.Name);
Form1.Label2.Caption:=StrPas(path)+'\'+searchRec.Name;
//Form1.StatusBar1.SimpleText:=StrPas(path)+'\'+searchRec.Name;
Application.ProcessMessages;
end;
found:=FindNext(searchRec);
end;
FindClose(searchRec);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin //指定分区查找
EnumFileInRecursion(Pchar(ComboBox1.Text));
Label1.Caption:=' 本次共扫描'+IntToStr(Memo1.Lines.Count);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
I: Integer; //全盘查找
Drive: PChar;
begin
for I := 0 to 31 do
begin
if Boolean(GetLogicalDrives and (1 SHL I)) then
begin
Drive:= PChar(CHR(65 + I) + ':\');
Memo1.Lines.Add( '正在查找'+Drive+'盘文件...' );
// CreateThread(nil,0,@TForm1.GetFile,pchar('c:\'),0,tid);
EnumFileInRecursion(Drive);
Memo1.Lines.Add( Drive+'盘文件查找完毕!' );
end;
end;
end;

相关阅读 >>

Delphi tadodataset 加载数据到fdmemtable1

Delphi pos的用法

Delphi中复制线程

Delphi xe5 android 程序退出功能

Delphi压缩图片代码

Delphi从路径中分离文件名

Delphi splitter 控件属性及作用

Delphi实现文件拖放代码

Delphi获得硬盘所有分区

Delphi tstringlist 用法详解

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



打赏

取消

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

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

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

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

评论

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