델파이
[델파이] 지정한 폴더에 들어 있는 파일을 표시2
띠리
2008. 10. 15. 17:20
지정한 폴더에 들어있는 파일을 검색해서 TStringList로 반환해주는 함수 예제
서브 폴더 밑의 파일까지도 파일을 까지도 검색하는 함수
// ************************************************************ // 지정한 폴더 밑에 파일을 필터링하여 // 지정한 폴더 밑의 파일 리스트를 얻는 함수 // ************************************************************ procedure GetSearchedFileList(sPath : String; slFileList : TStringList; sWildStr : string; bSchSubFolder : Bool); var sTempPath : String; SchRec : TSearchRec; iSchRec : integer; begin // 뒤에 '\' 붙이기 if sPath[length(sPath)] <> '\' then sPath := sPath + '\'; // 와일드 카드 설정 if (sWildStr = '') or (bSchSubFolder = true) then sTempPath := sPath + '*.*' else sTempPath := sPath + sWildStr; iSchRec := FindFirst(sTempPath, faAnyFile or faDirectory, SchRec); while (iSchRec = 0) do begin // '.', '..' 폴더는 제외 if not ((SchRec.Name = '.') or (SchRec.Name = '..')) then begin // 폴더가 아닌 것 if (SchRec.Attr and faDirectory) = 0 then begin // 와일드카드를 설정하고 서브폴더 밑도 검색할 경우 if (bSchSubFolder = true) and ((sWildStr <> '') or (sWildStr <> '*.*')) then begin // 수동 필터링 : *.확장자만 지원 / '?'는 지원안함 if ExtractFileExt(SchRec.Name) = ExtractFileExt(sWildStr) then slFileList.add(sPath + SchRec.Name); end // 일반적인 경우 : 서브폴더 밑 검색안함 else slFileList.add(sPath + SchRec.Name); end // 폴더인 경우 else begin // 폴더 안에 파일도 검색하는 경우 if bSchSubFolder = true then begin // 파일 검색 함수 : 재귀호출 GetSearchedFileList(sPath + SchRec.Name + '\', slFileList, sWildStr, bSchSubFolder); end; end; end; iSchRec := FindNext(SchRec); end; FindClose(SchRec); end; // ************************************************************ // 지정한 폴더 밑에 파일을 필터링하여 StringList로 반환 // ************************************************************ procedure GetFileList(sPath : String; slFileList : TStringList; sWildCard : string = ''; bSearchSubFolder : Bool = false); begin slFileList.Clear; GetSearchedFileList(sPath, slFileList, sWildCard, bSearchSubFolder); end; procedure TForm2.Button2Click(Sender: TObject); var slFileList : TStringList; begin // TStringList초기화 slFileList := TStringList.Create; GetSearchedFileList('D:\work', slFileList, '*.CSV', true); //GetFileList('D:\work', slFileList); ListBox1.Items := slFileList; end;