디렉토리에 파일이 있으면 지워지지 않는 바람에 귀찮아 졌다.

어떻게 해결 할까 하고 귀찮아서 뒹굴 거리다 찾은 소스..

아 귀찮어.~~~

실은 다른분이 찾아 주셨다…

ㅋㅋ

BOOL DeleteDirectory(const TCHAR* sPath) {
   HANDLE hFind;  // file handle
   WIN32_FIND_DATA FindFileData;

   TCHAR DirPath[MAX_PATH];
   TCHAR FileName[MAX_PATH];

   _tcscpy(DirPath,sPath);
   _tcscat(DirPath,”\\*”);    // searching all files
   _tcscpy(FileName,sPath);
   _tcscat(FileName,”\\”);

   hFind = FindFirstFile(DirPath,&FindFileData); // find the first file
   if(hFind == INVALID_HANDLE_VALUE) return FALSE;
   _tcscpy(DirPath,FileName);
       
   bool bSearch = true;
   while(bSearch) { // until we finds an entry
       if(FindNextFile(hFind,&FindFileData)) {
           if(IsDots(FindFileData.cFileName)) continue;
           _tcscat(FileName,FindFileData.cFileName);
           if((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {

               // we have found a directory, recurse
               if(!DeleteDirectory(FileName)) {
                   FindClose(hFind);
                   return FALSE; // directory couldn’t be deleted
               }
               RemoveDirectory(FileName); // remove the empty directory
               _tcscpy(FileName,DirPath);
           }
           else {
               if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
                   _chmod(FileName, _S_IWRITE); // change read-only file mode
               if(!DeleteFile(FileName)) {  // delete the file
                   FindClose(hFind);
                   return FALSE;
               }                 
               _tcscpy(FileName,DirPath);
           }
       }
       else {
           if(GetLastError() == ERROR_NO_MORE_FILES) // no more files there
           bSearch = false;
           else {
               // some error occured, close the handle and return FALSE
               FindClose(hFind);
               return FALSE;
           }

       }

   }
   FindClose(hFind);  // closing file handle

   return RemoveDirectory(sPath); // remove the empty directory

Leave a Reply

Your email address will not be published. Required fields are marked *