프로그래밍 노트

[델파이] 지정한 폴더의 파일 리스트 가져오기(서브 폴더 검색 대응) 본문

델파이

[델파이] 지정한 폴더의 파일 리스트 가져오기(서브 폴더 검색 대응)

띠리 2009. 7. 13. 15:46


 

// 지정한 폴더의 파일 리스트 가져오기 : 서브 폴더 검색 대응
function FindFiles(const sPath, sMask: string; slFiles: TStringList; bSubDir: boolean): integer;
var
  iFindResult: integer;
 srSchRec : TSearchRec;
begin
  result := 0;

  iFindResult := FindFirst(sPath + sMask, faAnyFile - faDirectory, srSchRec);
  while iFindResult = 0 do
  begin
    slFiles.Add(sPath + srSchRec.Name);
    iFindResult := FindNext(srSchRec);
  end;
  FindClose(srSchRec);

  if bSubDir then
  begin
    iFindResult := FindFirst(sPath + '*.*', faDirectory, srSchRec);
    while iFindResult = 0 do
    begin
      if (srSchRec.Name <> '.') and (srSchRec.Name <> '..') then
        result := result + FindFiles(sPath + srSchRec.Name + '\', sMask, slFiles, TRUE);
      iFindResult := FindNext(srSchRec);
    end;
    FindClose(srSchRec);
  end;
end;




procedure TForm1.Button1Click(Sender: TObject);
var
  sl: TStringList;
begin
  Button1.Enabled := false;
  sl := TStringList.Create;
  sl.Clear;
  FindFiles('D:\Data\', '*.doc', sl, True);
  Listbox1.Items := sl;
  sl.Free;
  Button1.Enabled := True;
end;

Comments