Skip to content

Commit

Permalink
fixed directory delete bug (had problems with nested directories).
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanjay0 committed Apr 18, 2016
1 parent 87d4682 commit 258a8bb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 27 deletions.
3 changes: 2 additions & 1 deletion MovieExplorer/ListView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ void CListView::OnCommand(WORD id, WORD notifyCode, HWND hWndControl)
if (MessageBox(GetMainWnd(), GETSTR(IDS_SURETODELETEDIRECTORY),
_T("Movie Explorer"), MB_ICONQUESTION | MB_YESNO) == IDYES)
{
RemoveDirectory(strFilePath, true); // Delete directory, all subdirectories, and files.
DeleteDirectoryAndAllSubfolders(strFilePath);
//RemoveDirectory(strFilePath, true); // Delete directory, all subdirectories, and files.
LOG(_T("Deleted Directory: ") + mov.strFileName + _T("\n"));
GetDB()->SyncAndUpdate();
}
Expand Down
41 changes: 15 additions & 26 deletions RClasses/general.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#define CHARSET_UTF8 4
#define CHARSET_UTF16 CHARSET_UTF16LE

#include <strsafe.h>

inline RString GetAppPath()
{
RString str;
Expand Down Expand Up @@ -94,37 +96,24 @@ inline RString GetDir(RString_ strFilePath)
return strDir;
}

inline bool RemoveDirectory(const TCHAR* lpszPathName, bool bDeleteContents)
{
if (!bDeleteContents)
return RemoveDirectory(lpszPathName) != FALSE;

RString strPathName(lpszPathName);
WIN32_FIND_DATA ffd;
HANDLE hFindFile = FindFirstFile(strPathName + _T("\\*.*"), &ffd);

if (hFindFile != INVALID_HANDLE_VALUE)
{
do
{
if (_tcscmp(ffd.cFileName, _T(".")) == 0 || _tcscmp(ffd.cFileName, _T("..")) == 0)
continue;

if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
if (!RemoveDirectory(strPathName + _T("\\") + ffd.cFileName, true))
{FindClose(hFindFile); return false;}

if (!DeleteFile(strPathName + _T("\\") + ffd.cFileName))
{FindClose(hFindFile); return false;}

} while (FindNextFile(hFindFile, &ffd));
inline LONG DeleteDirectoryAndAllSubfolders(const TCHAR* lpszDirectory)
{
TCHAR szDir[MAX_PATH + 1]; // +1 for the double null terminate
SHFILEOPSTRUCTW fos = { 0 };

FindClose(hFindFile);
}
StringCchCopy(szDir, MAX_PATH, lpszDirectory);
int len = lstrlenW(szDir);
szDir[len + 1] = 0; // double null terminate for SHFileOperation

return RemoveDirectory(lpszPathName) != FALSE;
// delete the folder and everything inside
fos.wFunc = FO_DELETE;
fos.pFrom = szDir;
fos.fFlags = FOF_NO_UI;
return SHFileOperation(&fos);
}


inline RString GetComputerName()
{
static RString str;
Expand Down

0 comments on commit 258a8bb

Please sign in to comment.