VCL 작업
이번에 TListView 컨트롤에 파일 목록을 표시하는 파일 리스트 프로그램을 만들어 보려고 합니다.
1. 폴더 선택하는 기능 구현
2. 폴더 정보를 넘기면 파일 목록을 가져오는 프로시져 만들기
3. 파일 리스트에서 파일 사이즈를 넘기면 파일 크기 계산해서 KB, MB,GB,TB 단위로 표시하는 함수 만들기
4. 리스트뷰(ListView) 컴포넌트에 표시
디자인 작업

Form - Caption : File List
Label1 - Caption : Path
txtPath - Text :
btnFolderSelect - Caption : Folder Select
Label1, txtPath, btnFolderSelect - 폰트(Font) 사이즈 14로 변경
ListView 컨트롤 사용

TListView 컴포넌트 팔레스 Win32 에 있습니다.
TListView : lstFileList
Columns 프로퍼티 설정

Column[0] - Caption : File Name, Width : 300
Column[1] - Caption : File Size, Width : 100
소스코드
버튼 클릭시 폴더를 선택하고 경로를 txtPath 에 넣고 프로시져 LoadFileList 호출 파라미터는 선택한 폴더 경로
procedure TForm1.btnFolderSelectClick(Sender: TObject);
begin
with TFileOpenDialog.Create(nil) do
try
Options := [fdoPickFolders];
if Execute then
txtPath.Text := FileName;
LoadFileList(FileName);
finally
Free;
end;
end;
ListView 컨트롤에 파익 목록을 표시하는 프로시져
type
TForm1 = class(TForm)
Label1: TLabel;
txtPath: TEdit;
btnFolderSelect: TButton;
lstFileList: TListView;
procedure btnFolderSelectClick(Sender: TObject);
private
{ Private declarations }
procedure LoadFileList(Path: String); // 파일 목록 표시 프로시져 선언
public
{ Public declarations }
end;
LoadFileList 구현
procedure TForm1.LoadFileList(Path: String);
var
SearchRec: TSearchRec;
ListItem: TListItem;
begin
if Path = '' then Exit;
lstFileList.Items.BeginUpdate;
lstFileList.Items.Clear;
// 모든 파일 리스트 구하기
if FindFirst(Path + '\*.*',faAnyFile,SearchRec) = 0 then begin
repeat
ListItem := lstFileList.Items.Add;
ListItem.Caption := SearchRec.Name;
Until (FindNext(SearchRec) <> 0);
FindClose(SearchRec);
end;
lstFileList.Items.EndUpdate;
end;
실행화면

파일 사이즈 가져오는 함수 추가
1024 byte 는 1 Kbyte = 1KB 로 표시
1024로 나눠서 사이즈를 계산한다.
function FileSizeFormat(Size: Double):String;
const
sUnit: array[0..3] of string = ('KB', 'MB', 'GB', 'TB');
var
nUnit: ShortInt;
nDec : Integer;
nTmp: Double;
begin
nUnit := 0;
nTmp := Round( Size / 1024);
while (nTmp > 1024) do begin
nTmp := nTmp / 1024;
Inc(nUnit);
end;
nDec := Integer(Trunc((nTmp * 10) - Trunc(nTmp) * 10) > 0);
Result := Format('%1.*n%s', [nDec,nTmp, sUnit[nUnit]]);
end;
파일 사이즈 가져오는 부분 LoadFileList 프로시져 수정
procedure TForm1.LoadFileList(Path: String);
var
SearchRec: TSearchRec;
ListItem: TListItem;
begin
if Path = '' then Exit;
lstFileList.Items.BeginUpdate;
lstFileList.Items.Clear;
// 모든 파일 리스트 구하기
if FindFirst(Path + '\*.*',faAnyFile,SearchRec) = 0 then begin
repeat
ListItem := lstFileList.Items.Add;
ListItem.Caption := SearchRec.Name;
// 파일 사이즈 표시
ListItem.SubItems.Add(FileSizeFormat(SearchRec.Size));
Until (FindNext(SearchRec) <> 0);
FindClose(SearchRec);
end;
lstFileList.Items.EndUpdate;
end;
실행결과

'Programming > Delphi' 카테고리의 다른 글
| [Delphi] 웹프로그래밍 #2 - 코딩 (0) | 2026.05.09 |
|---|---|
| [Delphi] 웹프로그래밍 #1 - IIS 설정 (0) | 2026.05.07 |
| 델파이 강좌 기초 #9 (문법 3) (0) | 2026.04.29 |
| 델파이 강좌 기초 #8 (문법 2) (0) | 2026.04.29 |
| 델파이 강좌 기초 #7 (문법 1) (2) | 2026.04.29 |