폴더안에 들어있는 파일중에서 최근에 저장된 파일명을 알고자 합니다.
A라는 파일이 있을대 최종적으로 수정된 날자, 시간를 프로그램에서는 어떻게 알수있나요.
함수같은것이 있을것 같은데요. 만약에 1.txt가 3월19일오전10시에 2.txt가 3월19일 오전11시에
생성되었다면 2.txt를 선택할수 있도록
findfirst를 치고 F1을 눌러보세요.
C Standard Library와 Visual Component
Library가 제공됩니다.
>> 추가 : 지나가는 사람님의 답변글처럼 F1을 눌러서 나오는 도움말을 적어본다.
◎ FindFirst
지정된 디렉토리에서 선택된 속성에 맞는 첫번째 파일 이름을 찾는다.
원형
extern PACKAGE int __fastcall FindFirst(const AnsiString Path, int Attr,
TSearchRec &F);
상세
FindFirst는 지정된 경로의 디렉토리에서 Attr 인자의 속성과 경로에 적합한 첫번째 파일을 찾는다. F 인자로 결과가
리턴된다. 필요한 정보를
뽑아내기 위해 찾은 결과 레코드의 필드가 사용되어진다. FindFirst는 찾기에 성공하면 0을 리턴하고, 그렇지못했다면,
Windows error code
를 리턴한다. const Path 인자는 디렉토리나 와일드카드 케릭터('*')를 포함한 파일이름
마스크이다. 예로, C:\test\*.* 라고 한다면 C:\test
디렉토리의 모든 파일을 지정한다.
Attr parameter
Constant
Value Description
faReadOnly
$00000001 Read-only
files
faHidden
$00000002 Hidden
files
faSysFile
$00000004
System
files
faVolumeID
$00000008
Volume ID files
faDirectory
$00000010
Directory files
faArchive
$00000020
Archive files
faAnyFile
$0000003F
Any
file
속성(Attributes)는 상수 또는 변수끼리 or연산자로 결합할 수 있다. 예를 들어, normal 파일에서 read-oly와
hidden 속성 가진 결과를 찾기위해서
Attr 인자를 faReadOnly | faHidden 과 같이 사용할 수 있다.
Note
FindFirst로 할당 받은 메모리는 FindClose로 해제해야한다.
FindFirst의 결과에서 이름과 속성이 적합한 다음 엔터리(next entery)값을 리턴한다.
원형
extern PACKAGE int __fastcall FindNext(TSearchRec &F);
상세
FindNext는 FindFirst의 결과에서 이름과 속성이 적합한 다음 엔터리(next entry)값을 리턴한다. 결과
레코드는 FindFirst의 결과내의 값이어야 한다.
다음 엔터리가 있다면 0을 리턴하고, 그렇지않으면 Windows error code를 리턴한다.
(ex) FindFirst, FindNext,
FindClose
1. New Application > CheckBox 7개 추가 > EditBox 1개 추가 > StringGrid
1개 추가 > Button 1개 추가
2. Button의 OnClick 이벤트에서 다음과 같이 코딩한다. (CheckBox에는 코드를 보고 적당한 Label을 넣기
바란다.)
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TSearchRec sr;
int iAttributes = 0;
StringGrid1->RowCount = 1;
iAttributes |= faReadOnly *
CheckBox1->Checked;
iAttributes |= faHidden *
CheckBox2->Checked;
iAttributes |= faSysFile *
CheckBox3->Checked;
iAttributes |= faVolumeID *
CheckBox4->Checked;
iAttributes |= faDirectory *
CheckBox5->Checked;
iAttributes |= faArchive *
CheckBox6->Checked;
iAttributes |= faAnyFile *
CheckBox7->Checked;
StringGrid1->RowCount = 0;
if
(FindFirst(Edit1->Text, iAttributes, sr) ==
0)
{
do
{
if ((sr.Attr & iAttributes) ==
sr.Attr)
{
StringGrid1->RowCount =
StringGrid1->RowCount + 1;
StringGrid1->Cells[1][StringGrid1->RowCount-1] =
sr.Name;
StringGrid1->Cells[2][StringGrid1->RowCount-1] =
IntToStr(sr.Size);
}
}
while (FindNext(sr) == 0);
FindClose(sr);
}
}
3. F9를 눌러 실행을 하고, EditBox에 'C:\*.*' 같이 원하는 형태의 Path 인자를 주고,
CheckBox를 선택한 후 Button을 누른다.
4. StringGrid에 파일이름과 사이즈가 제대로 들어갔는지 확인한다.