프로그래밍 노트

[Delphi] SubFolder 안의 File 검색하기 본문

카테고리 없음

[Delphi] SubFolder 안의 File 검색하기

띠리 2011. 4. 12. 19:32

// GetFileList : Sub Folder안의 File까지 검색하는 함수
//      slFileList : 반환하는 File List
//      sPath : 검색한 Root Folder
//      sMask : File 검색 Mask 설정
//              예) '*.txt', '*.exe'
//      bSubDir : SubFolder 검색 유무
procedure GetFileList(slFileList: TStringList;
                      sPath, sMask: string; bSubDir: boolean);
var
  i, iFindRst : integer;
  SrchRec : TSearchRec;
  slFolder : TStringList;
begin
  if sPath[length(sPath)] <> '\' then
    sPath := sPath + '\';

  // 지정 Foldr 안의 지정 Mask의 Full File path 수집
  iFindRst := FindFirst(sPath + sMask, faAnyFile - faDirectory, SrchRec);
  while iFindRst = 0 do
  begin
    slFileList.Add(sPath + SrchRec.Name);
    iFindRst := FindNext(SrchRec);
  end;
  FindClose(SrchRec);

  // Sub Folder List 수집
  slFolder := TStringList.Create;
  iFindRst := FindFirst(sPath + '*.*', faAnyFile, SrchRec);
  while iFindRst = 0 do begin
    if ((SrchRec.Attr and faDirectory) <> 0) and (SrchRec.Name[1] <> '.') then
      slFolder.Add(sPath + SrchRec.Name);
    iFindRst := FindNext(SrchRec);
  end;
  FindClose(SrchRec);

  // Sub Folder안 File 검색
  for i := 0 to slFolder.Count - 1 do
    GetFileList(slFileList, slFolder[i], sMask, bSubDir);

  slFolder.Free;
end;




procedure TForm1.Button1Click(Sender: TObject);
var
  slFiles : TStringList;
begin
  slFiles := TStringList.Create;
  GetFileList(slFiles, 'c:\data\', '*.txt', true); //

  memo1.Clear;
  memo1.Lines := slFiles;

  slFiles.Free;
end;

Comments