Search This Blog

Saturday, January 17, 2009

Function delete a Folder or directory with files and subfolders.

There is no API to delete a directory with files and subfolders.
It is the function to delete folders with files using shell operation.


bool DeleteDirectory(LPCTSTR lpszDir)
{
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


fileop.fAnyOperationsAborted = FALSE;
fileop.lpszProgressTitle     = NULL;
fileop.hNameMappings         = NULL;

int ret = SHFileOperation(&fileop); 
delete [] pszFrom;  
return (ret == 0);
}

This function return true if it succeed.

No comments: