본문 바로가기
My/works

[C] 디렉토리(폴더)의 모든 파일을 제거

by matt131 2012. 12. 26.
반응형

디렉토리(폴더)내의 모든 파일을 제거, 삭제하는 함수 DeleteAllFile 입니다.

파일을 삭제하는 함수이므로 주의해서 사용하시기 바랍니다.

핵심 코드는 CFileFind, FindFile, DeleteFile 입니다.

void DeleteAllFile( LPCSTR szDir ) 
{ 
    CString strName; 
    strName.Format( "%s\\*.*", szDir ); 
    CFileFind ff; 
    BOOL bFind = ff.FindFile( strName ); 

    while( bFind ) 
    { 
        bFind = ff.FindNextFile(); 
        if( ff.IsDots() == TRUE || ff.IsDirectory() == TRUE ) continue; 
        DeleteFile( ff.GetFilePath() ); 
    } 
    ff.Close(); 
}

 

사용법

매우 간단한 사용법을 가지고 있습니다.

DeleteAllFile( "C:\\temp" ); 

 

 

반응형