Procedure TForm1.FindDir(sDirName:String); {SDirName : 目录名, 例子: finddir('c:\temp') } var hFindFile:Cardinal; Sr:TSearchRec; sCurDir:String[255]; begin //先保存当前目录 sCurDir:=GetCurrentDir; ChDir(sDirName); hFindFile:=FindFirst('*.*',faAnyFile,Sr); If hFindFile<>INVALID_HANDLE_VALUE Then Begin repeat if (sr.Name='.') or (sr.Name='..') then Continue; //继续循环 //if (sr.FindData.dwFileAttributes and (1 shl 4))=FILE_ATTRIBUTE_DIRECTORY then if (sr.Attr and FILE_ATTRIBUTE_DIRECTORY)=FILE_ATTRIBUTE_DIRECTORY then
//是目录,则递归 FindDir(IncludeTrailingBackslash(sDirName)+sr.Name) else begin //是文件则显示 Form1.Memo1.Lines.Add(IncludeTrailingBackslash(sDirName)+sr.Name+' ('+Inttostr(sr.Size)+' bytes)'); end; Application.ProcessMessages; until FindNext(Sr)<>0; FindClose(Sr); End; //回到原来的目录下 ChDir(sCurDir); end;