Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Deleting a folder

Deleting a folder

Scheduled Pinned Locked Moved C / C++ / MFC
jsonhelpquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vinaya
    wrote on last edited by
    #1

    Hi, I created an application which creates a folder in the temp directory to store the temporary output files, at program startup. When I exit my application I want to delete the files as well as the folder. My application successfully deletes the files( I used DeleteFile() API), but not the directory. The RemoveDirectory() always fails. Why is it so? I have also tried using _rmdir() method.

    void RemoveTempFolder(LPCTSTR dirName)
    {
     basic_string stFolder;
     BOOL bFinish = TRUE;
     HANDLE	hFind;
     WIN32_FIND_DATA pFindData;
    
     stFolder.assign(dirName);
     stFolder.append(_T("\\*.*"));
     if((hFind = FindFirstFile (stFolder.c_str(), &pFindData))==  INVALID_HANDLE_VALUE)
     {
      return ;
     }
     while(bFinish)
     {
      if(_tcsicmp(pFindData.cFileName,_T(".")) != 0 &&   _tcsicmp(pFindData.cFileName,_T("..")) != 0)
      {
       basic_string sTemp;
       sTemp.assign(dirName);
       sTemp.append( _T("\\"));
       sTemp.append(pFindData.cFileName);
       DeleteFile(sTemp.c_str());
      } 
      bFinish = FindNextFile(hFind,&pFindData);
     }
     int nRet = RemoveDirectory(dirName);
     if(!nRet)
      DWORD dErr = GetLastError();
    }
    

    I also tried calling RemoveDirectory()from outside the function. Still the same result. :( Is there anything I am missing here?? Pls help. Thanks, Vini

    2 D 2 Replies Last reply
    0
    • V Vinaya

      Hi, I created an application which creates a folder in the temp directory to store the temporary output files, at program startup. When I exit my application I want to delete the files as well as the folder. My application successfully deletes the files( I used DeleteFile() API), but not the directory. The RemoveDirectory() always fails. Why is it so? I have also tried using _rmdir() method.

      void RemoveTempFolder(LPCTSTR dirName)
      {
       basic_string stFolder;
       BOOL bFinish = TRUE;
       HANDLE	hFind;
       WIN32_FIND_DATA pFindData;
      
       stFolder.assign(dirName);
       stFolder.append(_T("\\*.*"));
       if((hFind = FindFirstFile (stFolder.c_str(), &pFindData))==  INVALID_HANDLE_VALUE)
       {
        return ;
       }
       while(bFinish)
       {
        if(_tcsicmp(pFindData.cFileName,_T(".")) != 0 &&   _tcsicmp(pFindData.cFileName,_T("..")) != 0)
        {
         basic_string sTemp;
         sTemp.assign(dirName);
         sTemp.append( _T("\\"));
         sTemp.append(pFindData.cFileName);
         DeleteFile(sTemp.c_str());
        } 
        bFinish = FindNextFile(hFind,&pFindData);
       }
       int nRet = RemoveDirectory(dirName);
       if(!nRet)
        DWORD dErr = GetLastError();
      }
      

      I also tried calling RemoveDirectory()from outside the function. Still the same result. :( Is there anything I am missing here?? Pls help. Thanks, Vini

      2 Offline
      2 Offline
      224917
      wrote on last edited by
      #2

      Try after closing the search handle with FindClose().


      suhredayan
      There is no spoon.

      V 1 Reply Last reply
      0
      • 2 224917

        Try after closing the search handle with FindClose().


        suhredayan
        There is no spoon.

        V Offline
        V Offline
        Vinaya
        wrote on last edited by
        #3

        Thanks. But it didn't work. :( Still my temporary folder is not deleted. Vini

        1 Reply Last reply
        0
        • V Vinaya

          Hi, I created an application which creates a folder in the temp directory to store the temporary output files, at program startup. When I exit my application I want to delete the files as well as the folder. My application successfully deletes the files( I used DeleteFile() API), but not the directory. The RemoveDirectory() always fails. Why is it so? I have also tried using _rmdir() method.

          void RemoveTempFolder(LPCTSTR dirName)
          {
           basic_string stFolder;
           BOOL bFinish = TRUE;
           HANDLE	hFind;
           WIN32_FIND_DATA pFindData;
          
           stFolder.assign(dirName);
           stFolder.append(_T("\\*.*"));
           if((hFind = FindFirstFile (stFolder.c_str(), &pFindData))==  INVALID_HANDLE_VALUE)
           {
            return ;
           }
           while(bFinish)
           {
            if(_tcsicmp(pFindData.cFileName,_T(".")) != 0 &&   _tcsicmp(pFindData.cFileName,_T("..")) != 0)
            {
             basic_string sTemp;
             sTemp.assign(dirName);
             sTemp.append( _T("\\"));
             sTemp.append(pFindData.cFileName);
             DeleteFile(sTemp.c_str());
            } 
            bFinish = FindNextFile(hFind,&pFindData);
           }
           int nRet = RemoveDirectory(dirName);
           if(!nRet)
            DWORD dErr = GetLastError();
          }
          

          I also tried calling RemoveDirectory()from outside the function. Still the same result. :( Is there anything I am missing here?? Pls help. Thanks, Vini

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          Vinaya wrote: DWORD dErr = GetLastError(); What is the value of dErr? That would be a big clue as to why RemoveDirectory() is not working.


          "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups