When we delete a directory programatically we can't delete it when there is some files or folders inside the directory. We can delete only the empty folders.
Otherwise, We usually delete recursively search and delete all files and then we delete all folders and finally we delete the folder which we want to delete.
But the following code snippet uses shell operation and delete the directory directly without any recursive function.
It is faster than the recursive function.
Otherwise, We usually delete recursively search and delete all files and then we delete all folders and finally we delete the folder which we want to delete.
But the following code snippet uses shell operation and delete the directory directly without any recursive function.
It is faster than the recursive function.
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); }
No comments:
Post a Comment