程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 遍歷指定目錄下指定類型文件的函數

遍歷指定目錄下指定類型文件的函數

編輯:Delphi
// ================================================================ 
// 遍歷某個文件夾下某種文件, 
// 使用說明 
//    _GetFileList(ListBox1.Items,'c:\*.doc'); 
//      _GetFileList(MyTStringList,'c:\*.exe'); 
// ================================================================ 
procedure TForm1._GetFileList(AStrings: TStrings ; ASourFile: string); 
var sour_path,sour_file: string; 
  TmpList:TStringList; 
  FileRec:TSearchrec; 
begin 
 
  sour_path:=ExtractFilePath(ASourFile); 
  sour_file:=ExtractFileName(ASourFile); 
 
  if not DirectoryExists(sour_path) then 
  begin 
   AStrings.Clear; 
   exit; 
  end; 
 
  TmpList:=TStringList.Create; 
  TmpList.Clear; 
 
  if FindFirst(sour_path+sour_file,faAnyfile,FileRec) = 0 then 
  repeat 
   if ((FileRec.Attr and faDirectory) = 0) then 
     begin 
      TmpList.Add(sour_path+FileRec.Name) 
     end; 
  until FindNext(FileRec)<>0; 
 
  SysUtils.FindClose(FileRec); 
 
  AStrings.Assign(TmpList); 
 
  TmpList.Free; 
end; 
 
// ================================================================ 
// 遍歷某個文件夾及子文件夾下某種文件, 
// 使用說明 
//    _GetFileList(ListBox1.Items, 'c:\', '*.doc'); 
//      _GetFileList(MyTStringList, 'c:\', '*.exe'); 
// ================================================================ 
procedure _GetFileList(AStrings: TStrings; ASourFile, 
 FileName: string); 
var sour_path,sour_file: string; 
  TmpList:TStringList; 
  FileRec, subFileRec:TSearchrec; 
  i: Integer; 
begin 
  if rightStr(trim(ASourFile), 1) <> '\' then 
   sour_path :=trim(ASourFile) + '\' 
  else 
   sour_path :=trim(ASourFile); 
  sour_file:= FileName; 
 
  if not DirectoryExists(sour_path) then 
  begin 
   AStrings.Clear; 
   exit; 
  end; 
 
  TmpList:=TStringList.Create; 
  TmpList.Clear; 
 
  if FindFirst(sour_path+'*.*',faAnyfile,FileRec) = 0 then 
  repeat 
   if ((FileRec.Attr and faDirectory) <> 0) then 
     begin 
      if ((FileRec.Name<> '.') and (FileRec.Name <> '..')) then 
       _GetFileList(AStrings, sour_path+ FileRec.Name + '\', sour_file); 
     end 
   else 
    if FindFirst(sour_path + FileName,faAnyfile,subFileRec) = 0 then 
    repeat 
     if ((subFileRec.Attr and faDirectory) = 0) then 
      TmpList.Add(sour_path+subFileRec.Name); 
    until FindNext(subFileRec)<>0; 
 
  until FindNext(FileRec)<>0; 
 
  FindClose(FileRec); 
  for i := 0 to TmpList.Count -1 do 
   AStrings.Add(TmpList.Strings[i]); 
 
  TmpList.Free; 
end; 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved