How to remove directories using c++ in windows -


i'm trying remove directories using code, wouldn't work. and, couldn't found issue. maybe lack of privileges on deletion operation on windows?

here code i'm using:

#define _crt_secure_no_deprecate #include <string> #include <iostream>  #include <windows.h> #include <conio.h>   int deletedirectory(const std::string &refcstrrootdirectory,     bool              bdeletesubdirectories = true) {     bool            bsubdirectory = false;       // flag, indicating whether     // subdirectories have been found     handle          hfile;                       // handle directory     std::string     strfilepath;                 // filepath     std::string     strpattern;                  // pattern     win32_find_data fileinformation;             // file information       strpattern = refcstrrootdirectory + "\\*.*";     hfile = ::findfirstfile((lpcwstr)strpattern.c_str(), &fileinformation);     if (hfile != invalid_handle_value)     {                 {             if (fileinformation.cfilename[0] != '.')             {                 strfilepath.erase();                 strfilepath = refcstrrootdirectory + "\\" + (char*)fileinformation.cfilename;                  if (fileinformation.dwfileattributes & file_attribute_directory)                 {                     if (bdeletesubdirectories)                     {                         // delete subdirectory                         int irc = deletedirectory(strfilepath, bdeletesubdirectories);                         if (irc)                             return irc;                     }                     else                         bsubdirectory = true;                 }                 else                 {                     // set file attributes                     if (::setfileattributes((lpcwstr)strfilepath.c_str(),                         file_attribute_normal) == false)                         return ::getlasterror();                      // delete file                     if (::deletefile((lpctstr)strfilepath.c_str()) == false)                         return ::getlasterror();                 }             }         } while (::findnextfile(hfile, &fileinformation) == true);          // close handle         ::findclose(hfile);          dword dwerror = ::getlasterror();         if (dwerror != error_no_more_files)             return dwerror;         else         {             if (!bsubdirectory)             {                 // set directory attributes                 if (::setfileattributes((lpcwstr)refcstrrootdirectory.c_str(),                     file_attribute_normal) == false)                     return ::getlasterror();                  // delete directory                 if (::removedirectory((lpcwstr)refcstrrootdirectory.c_str()) == false)                     return ::getlasterror();             }         }     }      return 0; }   int main() {     int         irc = 0;     std::string strdirectorytodelete = "c:\\users\\abysco\\desktop\\del";       // delete 'c:\mydir' without deleting subdirectories     irc = deletedirectory(strdirectorytodelete, false);     if (irc)     {         std::cout << "error " << irc << std::endl;         return -1;     }      // delete 'c:\mydir' , subdirectories     irc = deletedirectory(strdirectorytodelete);     if (irc)     {         std::cout << "error " << irc << std::endl;         return -1;     }      // wait keystroke     _getch();      return 0; } 

os: windows 7 sp1.

any brilliant idea, please?

the windows api comes function delete entire directories. shfileoperation. , on vista or later use ifileoperation same purpose.

not make process trivial, not more 1 liner, has other benefits:

  1. you can put deleted directory recycle bin if choose.
  2. you can show standard system progress dialog if choose.

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -