MFC에서 디렉토리 존재 유무 확인/디렉토리내 파일 검색


CFileFind란 Class를 사용하면 된다.

CFileFind cFileFinder;
BOOL bResult;

// path를 찾음 성공시 TRUE, 실패시 FALSE
bResult = cFileFinder.FindFile( path );
// 그 다음 같은 이름의 파일 혹은 디렉토리있으면 TRUE, 없으면 FALSE
bResult = cFileFinder.FindNextFile();

if( cFileFinder.IsDirectory() == TRUE )
MessageBox( "디렉토리 존재함" );

FindNextFile을 하지 않고 IsDirectory() 함수를 호출하면 에러가 떠버린다.

아래와 같은 와일드 카드도 사용할 수 있다.

[CODE type=cpp]// C:\\*.*가 있으면 TRUE, C:\의 어떤 파일이라도 있으면 TRUE
bResult = cFileFinder.FindFile( "C:\\*.*" );
// 바로 GetFileName하면 NULL에러 발생 아직 파일을 찾은 것은 아님
bResult = cFileFinder.FindNextFile();
MessageBox( cFileFinder.GetFileName() );[/CODE]

첫 번째 파일 이름이 나옴
그 다음은 FindNextFile()함수를 호출하여 리턴값이 0이 나올때까지 돌리면 된다.
MSDN예제엔 아래와 같이 쓰도록 되어있다.

[CODE type=cpp]bResult = cFileFinder.FindFile( "C:\\*.*" )

while( bResult )
{
bResult = cFileFinder.FindNextFile();

// . 이나 ..이면 다시 돌림
if( cFileFinder.IsDot() ) continue;

MessageBox( cFileFinder.GetFileName() );
}[/CODE]

참고 : MSDN, 데브피아 여러 자료


Powered by Tattertools