function Tform1.GetDirectoryName(Dir:String):String; //设置DIR
begin
if Dir[LengTh(Dir)]<>'\' then
Result:=Dir+'\'
else
Result:=Dir;
end;
procedure Tform1.FindFiles(APath:String); //查找主函数
var
FSearchRec,DsearchRec:TSearchRec;
FindResult:integer;
it:tlistitem;
fhandle:thandle;
hip:pointer;
filesize:longint;
/////////
tempstr:string;
function IsDirNotation(ADirName:String):Boolean;
begin
Result:=(ADirName='.') or(ADirName='..')or(ADirName='') ;
End;
begin
Apath:=GetDirectoryName(APath);
FIndResult:=FindFirst(APath+'*.*',faAnyFile+faHidden+faSysFile+FaReadonly,FsearchRec);
Try
while FindResult=0 do
begin
it:=listview1.Items.Add;
it.Caption:=extractfilename(APath+FSearchRec.Name); //把查找到的文件列出来
it.SubItems.Add(APath+FSearchRec.Name);
fhandle:=createfile(pchar(APath+FSearchRec.Name),GENERIC_READ,FILE_SHARE_READ,nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
new(hip);
filesize:=GetFileSize(fhandle,hip);
dispose(hip);
it.SubItems.Add(inttostr(filesize));
//获得所选路径以下文件路径
//tempint:=pos(nowdir,copy(apath,pathpos,length(apath)-pathpos+1));
//tempstr:=copy(apath,tempint+pathpos,length(apath)-tempint-pathpos);
tempstr:=copy(apath,pathpos,length(apath)-pathpos+1);
it.SubItems.Add(selectpath+tempstr+it.Caption);
closehandle(fhandle);
FindResult:=FindNext(FSearchRec);
end;
FindReSult:=FindFirst(APath+'*.*',faDirectory,DSearchRec);
while FindResult=0 do
begin
if (DSearchRec.Attr{ and faDirectory)}=faDirectory) and not ISDirNotation(DSEarchRec.Name) then
FindFiles(APath+DsearchRec.Name);
FindResult:=FindNext(DsearchREc);
end;
Finally
FindClose(FSearchRec);
findclose(DsearchREc);
end;
end;