title: RemoveDirectory() function in win32 api doesn't remove parent directory after removing subdirectories
body: <p>I'm having a problem removing a directory after it's subfolders / files have been removed. I'm using FindFirstFile, FindNextFile, RemoveDirectory, etc. For instance I have this directory:</p>
  
  <blockquote>
    <p>C:\Users\user\Desktop\SearchFile\ipch\searchfile-e3798fd4\some-files-here.ipch</p>
  </blockquote>
  
  <p>After my program runs, it's left with this</p>
  
  <blockquote>
    <p>C:\Users\user\Desktop\SearchFile\ipch</p>
  </blockquote>
  
  <p>As you can see it deletes all files in searchfile-e3798fd4 and removes the directory, but leaves the parent directory 'ipch', which i want deleted as well. This is my full current code:</p>
  
  <pre><code>#include &lt;Windows.h&gt;
  #include &lt;wchar.h&gt;
  #include &lt;stdio.h&gt;
  
  bool DeleteDirectory( const wchar_t *sDir )
  {
      WIN32_FIND_DATA fdFile;
      HANDLE hFind;
      wchar_t sPath[ MAX_PATH * 10 ];
  
      wsprintf( sPath, L"%s\\*.*", sDir );
  
      if( ( hFind = FindFirstFile( sPath, &amp;fdFile ) ) == INVALID_HANDLE_VALUE )
      {
          return false;
      }
  
      do
      {
          if( wcscmp( fdFile.cFileName, L"." ) != 0 &amp;&amp;
              wcscmp( fdFile.cFileName, L".." ) != 0 )
          {
              wsprintf( sPath, L"%s\\%s", sDir, fdFile.cFileName );
  
              if( fdFile.dwFileAttributes &amp; FILE_ATTRIBUTE_DIRECTORY )
              {
                  // Is Directory
                  RemoveDirectory( sPath );
                  DeleteDirectory( sPath );
              }
          }
      } while( FindNextFile( hFind, &amp;fdFile ) );
  
      FindClose( hFind );
      return true;
  }
  bool DeleteFiles( const wchar_t *sDir, WIN32_FIND_DATA fdFile, HANDLE hFind )
  {
      wchar_t sPath[ MAX_PATH * 10 ];
  
      wsprintf( sPath, L"%s\\*.*", sDir );
  
      if( ( hFind = FindFirstFile( sPath, &amp;fdFile ) ) == INVALID_HANDLE_VALUE )
      {
          MessageBox( NULL, L"123", L"123", MB_OK );
          return false;
      }
  
      do
      {
          if( wcscmp( fdFile.cFileName, L"." ) != 0 &amp;&amp;
              wcscmp( fdFile.cFileName, L".." ) != 0 )
          {
              wsprintf( sPath, L"%s\\%s", sDir, fdFile.cFileName );
  
              if( fdFile.dwFileAttributes &amp; FILE_ATTRIBUTE_DIRECTORY )
              {
                  // Is Directory
                  DeleteFiles( sPath, fdFile, hFind );
              }
              else
              {
                  // Is File
                  DeleteFile( sPath );
              }
          }
      } while( FindNextFile( hFind, &amp;fdFile ) );
  
  
      FindClose( hFind );
      return true;
  }
  bool DeleteFolderContents( const wchar_t *sDir )
  {
      WIN32_FIND_DATA fdFile;
      HANDLE hFind = NULL;
      wchar_t sPath[ MAX_PATH * 10 ];
  
      wsprintf( sPath, L"%s\\*.*", sDir );
  
      if( ( hFind = FindFirstFile( sPath, &amp;fdFile ) ) == INVALID_HANDLE_VALUE )
      {
          MessageBox( NULL, L"", L"", MB_OK );
          return false;
      }
  
      do
      {
          if( wcscmp( fdFile.cFileName, L"." ) != 0 &amp;&amp;
              wcscmp( fdFile.cFileName, L".." ) != 0 )
          {
              wsprintf( sPath, L"%s\\%s", sDir, fdFile.cFileName );
  
              if( fdFile.dwFileAttributes &amp; FILE_ATTRIBUTE_DIRECTORY )
              {
                  // Is Directory
                  if( wcscmp( fdFile.cFileName, L"Debug" ) == 0 ||
                      wcscmp( fdFile.cFileName, L"Release" ) == 0 ||
                      wcscmp( fdFile.cFileName, L"ipch" ) == 0 )
                  {
                      DeleteFiles( sPath, fdFile, hFind );
                  }
                  DeleteFolderContents( sPath );
              }
          }
      } while( FindNextFile( hFind, &amp;fdFile ) );
  
      FindClose( hFind );
      return true;
  }
  bool ListDirectoryContents( const wchar_t *sDir )
  {
      WIN32_FIND_DATA fdFile;
      HANDLE hFind = NULL;
      wchar_t sPath[ MAX_PATH * 10 ];
  
      wsprintf( sPath, L"%s\\*.*", sDir );
  
      if( ( hFind = FindFirstFile( sPath, &amp;fdFile ) ) == INVALID_HANDLE_VALUE )
      {
          MessageBox( NULL, L"Path not found", L"Error", MB_OK | MB_ICONERROR );
          return false;
      }
  
      do
      {
          if( wcscmp( fdFile.cFileName, L"." ) != 0 &amp;&amp;
              wcscmp( fdFile.cFileName, L".." ) != 0 )
          {
              wsprintf( sPath, L"%s\\%s", sDir, fdFile.cFileName );
  
              if( fdFile.dwFileAttributes &amp; FILE_ATTRIBUTE_DIRECTORY )
              {
                  // Is Directory
                  if( wcscmp( fdFile.cFileName, L"Debug" ) == 0 ||
                      wcscmp( fdFile.cFileName, L"Release" ) == 0 ||
                      wcscmp( fdFile.cFileName, L"ipch" ) == 0 )
                  {
                      // Working in debug folder
                      wprintf( L"[+] %s\n", sPath );
                  }
                  ListDirectoryContents( sPath );
              }
              else
              {
                  // Is File
                  wprintf( L"[-] %s\n", sPath );
              }
          }
      } while( FindNextFile( hFind, &amp;fdFile ) );
  
      FindClose( hFind );
      return true;
  }
  int main()
  {
      wchar_t pathName[] = L"C:\\Users\\user\\Desktop\\SearcFile\\";
  
      // List Directory Contents
      ListDirectoryContents( pathName );
      wprintf( L"\n" );
      system("pause");
  
      // Delete Folder Contents
      DeleteFolderContents( pathName );
      DeleteDirectory( pathName );
      wprintf( L"\n" );
      system("pause");
  
      ListDirectoryContents( pathName );
      wprintf( L"\n" );
      system("pause");
  
      return 0;
  }
  </code></pre>
  
  <p>I'm aware that the problem exists where I actually RemoveDirectory( sPath ); Because that function is called it sees that ipch isn't empty, therefore doesn't delete it. then recursives and continues on.</p>