카테고리 없음
Directory 생성, 삭제
코드베어
2020. 5. 7. 19:35
MFC 환경에서 Directory를 생성 및 삭제하는 예제이다.
- Directory 생성
BOOL MakeDir()
{
USES_CONVERSION;
BOOL bResult = FALSE;
do
{
if( -1 != access("c:\\mydir", 0) ) {
bResult = TRUE;
break;
}
if( !CreateDirectory( L"c:\\mydir", NULL) ) {
break;
}
bResult = TRUE;
}while(false);
return bResult;
}
|
access() 함수로 특정 디렉터리로가 존재하는 지 체크하고 없으면 CreateDriectory() 함수를 이용하여 디렉터리를 생성한다.
- Directory 삭제 ( 안에 있는 파일까지 다 지운다 )
BOOL DeleteDir()
{
TCHAR szPath[MAX_PATH] = {0, };
_stprintf_s( szPath, MAX_PATH, L"%s\\*.*", L"c:\\mydir" );
BOOL bResult = FALSE;
do
{
CFindFile finder;
BOOL bExist = finder.FindFile( szPath );
while( bExist )
{
bExist = finder.FindNextFile();
if( TRUE == finder.IsDots() ) {
continue;
} else if ( TRUE == finder.IsDirectory() ) {
RemoveDirectory( finder.GetFilePath() );
continue;
}
DeleteFile( finder.GetFilePath() );
}
finder.Close();
RemoveDirectory( L"c:\\mydir" );
bResult = TRUE;
} while( false );
return bResult;
}
|
CFindFile를 이용하여 디렉터리(\mydir) 안의 파일(&디렉터리)들을 순차적으로 검색&삭제하고 마지막 상위 디렉터리(\mydir)를 지운다.