CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Threaded View

  1. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360
    Q: How to delete a directory and its subdirectories using the Shell API?

    A: You can use 'SHFileOperation', which copies, removes, renames or deletes a file system object. In the example below it is used to delete a folder in a recursive manner:

    Code:
    #include <windows.h>
    #include <tchar.h>
    #include <shellapi.h>
    
    bool DeleteDirectory(LPCTSTR lpszDir, bool noRecycleBin = true)
    {
      int len = _tcslen(lpszDir);
      TCHAR *pszFrom = new TCHAR[len+2];
      _tcscpy(pszFrom, lpszDir);
      pszFrom[len] = 0;
      pszFrom[len+1] = 0;
      
      SHFILEOPSTRUCT fileop;
      fileop.hwnd   = NULL;    // no status display
      fileop.wFunc  = FO_DELETE;  // delete operation
      fileop.pFrom  = pszFrom;  // source file name as double null terminated string
      fileop.pTo    = NULL;    // no destination needed
      fileop.fFlags = FOF_NOCONFIRMATION|FOF_SILENT;  // do not prompt the user
      
      if(!noRecycleBin)
        fileop.fFlags |= FOF_ALLOWUNDO;
    
      fileop.fAnyOperationsAborted = FALSE;
      fileop.lpszProgressTitle     = NULL;
      fileop.hNameMappings         = NULL;
    
      int ret = SHFileOperation(&fileop);
      delete [] pszFrom;  
      return (ret == 0);
    }
    
    int main() 
    {
      DeleteDirectory("d:\\Test", false);
      return 0;
    }
    Last edited by Andreas Masur; July 24th, 2005 at 05:20 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured