CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 1999
    Posts
    69

    Implementing RemoveDirectory to handle non empty directories

    Hi,

    I needed to write a routine to delete a directory (even if it contained files and directories) which the Win32 API couldn't do. I digged up a recursive directory walker class that I had which supports bottom up recursion and used :eleteFile and ::RemoveDirectory on the walk back up the tree.

    The algorithm is correct, but sometimes the RemoveDirectory call is returning DIRECTORY_NOT_EMPTY.

    If I press refresh on explorer, I can visually see that the directory isn't empty.

    If I insert a Sleep command within the tree walk, the directories all delete okay.

    I've tried adding _flushall commands but that doesn't seem to have any effect.

    Anyway, as I needed the algorithm for work, I sorted it using a brute force algorithm (i.e. running the recursion a number of times until all
    the directories and files are deleted).

    But I'd like to know why the RemoveDirectory sometimes returns DIRECTORY_NOT_EMPTY (probably because of caching) and how to flush the cache (or whatever is needed).

    Thanks
    Rob.


  2. #2
    Join Date
    Apr 1999
    Posts
    396

    Re: Implementing RemoveDirectory to handle non empty directories

    BOOL WINAPI DeleteDirectory(LPCTSTR lpszDir)
    {
    WIN32_FIND_DATA find;
    BOOL bFound=TRUE;
    HANDLE hFind=INVALID_HANDLE_VALUE;

    ZeroMemory(&find,sizeof(find));

    if (!SetCurrentDirectory(lpszDir)) return FALSE;

    hFind=FindFirstFile("*.*",&find);
    if (hFind == INVALID_HANDLE_VALUE)
    {
    SetCurrentDirectory("..");
    return FALSE;
    }

    while (bFound)
    {
    if (find.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    {
    if ((lstrcmp(find.cFileName,".") != 0) && (lstrcmp(find.cFileName,"..") != 0))
    {
    DeleteDirectory(find.cFileName);
    }
    }
    else DeleteFile(find.cFileName);
    bFound=FindNextFile(hFind,&find);
    }
    FindClose(hFind);
    SetCurrentDirectory("..");
    return RemoveDirectory(lpszDir);
    }


  3. #3
    Join Date
    May 1999
    Posts
    69

    Re: Implementing RemoveDirectory to handle non empty directories

    This is pretty much the algorithm that I have got. The only interesting difference is that I'm not using Get/SetCurrentDirectory. Unfortunately, the app I've got is multithreaded with god knows how many different people accessing the file system, so we're banned from playing with the shared current directory resource.

    Do you think the SetCurrentDirectory call could be flushing things ?




  4. #4
    Join Date
    Apr 1999
    Posts
    396

    Re: Implementing RemoveDirectory to handle non empty directories

    To flush what? I don't think it causes any flushing of the file system. The call to SetCurrentDirectory just allows me to delete files without giving a full path name. So when i recurse, i just change the directories and I don't have to worry about maintaining a string that has the current path in it.


  5. #5
    Join Date
    Mar 2006
    Posts
    8

    Question Re: Implementing RemoveDirectory to handle non empty directories

    Hello,

    Me too, Iam getting the same error, in my case iam using ZwSetInformationFile to set the delete flag on the directory.

    But this call is failing by saying that DIRECOTRY_NOT_EMPTY ...even though i have called the ZwSetInformationFile which sets delete flag on all the files inside the direcotry. can you please tell me where exactly you are using the Sleep with in the tree walk. I mean are you first recursively trying to delete the directory, sleep,and then again recursively delete the directory?

  6. #6
    Join Date
    May 2005
    Posts
    4,954

    Re: Implementing RemoveDirectory to handle non empty directories

    Quote Originally Posted by Rob Wainwright
    Hi,

    I needed to write a routine to delete a directory (even if it contained files and directories) which the Win32 API couldn't do. I digged up a recursive directory walker class that I had which supports bottom up recursion and used :eleteFile and ::RemoveDirectory on the walk back up the tree.
    use ::SHFileOperation().

    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

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