如何遍历整个硬盘查找某一个文件,并返回所有绝对路径(30分)

  • 主题发起人 主题发起人 jsyzzdj
  • 开始时间 开始时间
J

jsyzzdj

Unregistered / Unconfirmed
GUEST, unregistred user!
就像windows 文件搜索-----------急不可待,在线等,
 
只能一个个的找了:
procedure SearchDir(WatchSubTree:Boolean;MainPath:String;SubTree:Boolean);
var i:Integer;
Found:Boolean;
SubDir:TSTringList;
SearchRec:TsearchRec;
S:String;
begin
if DirectList=nil then DirectList:=TStringList.Create;
if not SubTree then DirectList.Clear;
SubDir:=TStringList.Create;
if MainPath[Length(MainPath)]='/' then S:=MainPath
else S:=MainPath+'/';
if DirectoryExists(MainPath) then begin
//第一次遍历时加上本目录
if DirectList.Count=0 then DirectList.Add(MainPath+'/');
//找出所有下级子目录
if (FindFirst(S+'*.*', faDirectory, SearchRec)=0) then begin
if IsValidDir(SearchRec) then begin
SubDir.Add(SearchRec.Name);
DirectList.Add(S+SearchRec.Name+'/');
end;
while (FindNext(SearchRec) = 0) do begin
if IsValidDir(SearchRec) then begin
SubDir.Add(SearchRec.Name);
DirectList.Add(S+SearchRec.Name+'/');
end;
end;
end;
end;
if WatchSubTree then begin
//递归:查找各子目录
for i:=0 to SubDir.Count-1 do
SearchDir(True,S+SubDir.Strings+'/',True);
end;
end;
procedure GetAllFileNames(SearchDir:Boolean;WatchSubTree:Boolean=False;FileNameExt:String='');
var SearchRec: TSearchRec;
TempList:TStrings;
P:PFileCollection;
i,iExt:Integer;
J:Boolean;
SL:TStringList;
ValidateDir:Boolean;
S:String;
begin
if FileList<>nil then begin
for i:=0 to FileList.Count-1 do begin
P:=PFileCollection(FileList);
Dispose(P);
end;
FreeAndNil(FileList);
end;
FileList:=TList.Create;
SL:=TStringList.Create;
if Trim(FileNameExt)='' then exit;
splite(UpperCase(Trim(FileNameExt)),',',SL);
for iExt:=0 to SL.Count-1 do begin
for i:=0 to DirectList.Count-1 do begin
if not SearchDir then ValidateDir:=True
else begin
if Uppercase(Trim(GetFactPath(DirectList)))=
Uppercase(Trim(GetFactPath(SysDateDir))) then ValidateDir:=True
else ValidateDir:=False;
end;
if ValidateDir then begin
S:=GetFactPath(DirectList)+'*.'+Trim(SL[iExt]);
if FindFirst(S,faArchive,SearchRec)=0 then begin
J:=True;
New(P);
P.FileName:=SearchRec.Name;
P.DirPath:=DirectList;
P.FileSize:=SearchRec.Size;
P.ModifyTime:=FileDateToDateTime(SearchRec.Time);
FileList.Add(P);
while FindNext(SearchRec)=0 do begin
New(P);
P.FileName:=SearchRec.Name;
P.DirPath:=DirectList;
P.FileSize:=SearchRec.Size;
P.ModifyTime:=FileDateToDateTime(SearchRec.Time);
FileList.Add(P);
end;
end;
end;
end;
end;
//FindClose(SearchRec);
end;
 
改造了一段代码,你自己测试,应该可以。
//判斷是不是目錄
function IsValidDir(SearchRec:TSearchRec):Boolean;
begin
result:=(SearchRec.Attr and faDirectory=faDirectory)and(SearchRec.Name<>'.')and(SearchRec.Name<>'..');
end;

function SearchAllPath(Path:string; var filename:string):Boolean;
var
i:Integer;
sl_SubDir:TStrings;
SearchRec:TSearchRec;
begin
// CheckStop;
if Trim(Path)='' then exit;
sl_SubDir:=TStringList.Create;
if (FindFirst(Path+'/*.*',faDirectory,SearchRec)=0) then
begin
if IsValidDir(SearchRec) then sl_SubDir.Add(SearchRec.Name);
while (FindNext(SearchRec)=0) do
begin
if IsValidDir(SearchRec) then
sl_SubDir.Add(SearchRec.Name)
else if uppercase(SearchRec.Name)=uppercase(filename) then
begin
// filename:=Path+'/'+SearchRec.Name; //注意,这里的path就是你要的绝对目录
filename:=Path+'/'; //注意,只返回绝对目录 SearchAllPath:=true;
exit;
end;
end;
end;
FindClose(SearchRec);
for i:=0 to sl_SubDir.Count-1 do
begin
if SearchAllPath(Path+'/'+sl_SubDir.Strings,filename) then exit;
end;
sl_SubDir.Free;
end;
procedure TForm1.Button1Click(Sender:TObject);
var filename:string;
begin
filename:='lm.dat';
SearchAllPath('c:',filename); //你调用这个过程就可以,filename返回目录和文件,你也可以只是返回目录。
Label1.Caption:=filename;
end;
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
1K
import
I
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部