【发布时间】:2014-02-18 20:54:16
【问题描述】:
在删除子文件夹/文件后删除目录时遇到问题。我正在使用 FindFirstFile、FindNextFile、RemoveDirectory 等。例如我有这个目录:
C:\Users\user\Desktop\SearchFile\ipch\searchfile-e3798fd4\some-files-here.ipch
我的程序运行后,就剩下这个了
C:\Users\user\Desktop\SearchFile\ipch
如您所见,它删除了 searchfile-e3798fd4 中的所有文件并删除了该目录,但保留了父目录“ipch”,我也想将其删除。这是我当前的完整代码:
#include <Windows.h>
#include <wchar.h>
#include <stdio.h>
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, &fdFile ) ) == INVALID_HANDLE_VALUE )
{
return false;
}
do
{
if( wcscmp( fdFile.cFileName, L"." ) != 0 &&
wcscmp( fdFile.cFileName, L".." ) != 0 )
{
wsprintf( sPath, L"%s\\%s", sDir, fdFile.cFileName );
if( fdFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
{
// Is Directory
RemoveDirectory( sPath );
DeleteDirectory( sPath );
}
}
} while( FindNextFile( hFind, &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, &fdFile ) ) == INVALID_HANDLE_VALUE )
{
MessageBox( NULL, L"123", L"123", MB_OK );
return false;
}
do
{
if( wcscmp( fdFile.cFileName, L"." ) != 0 &&
wcscmp( fdFile.cFileName, L".." ) != 0 )
{
wsprintf( sPath, L"%s\\%s", sDir, fdFile.cFileName );
if( fdFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
{
// Is Directory
DeleteFiles( sPath, fdFile, hFind );
}
else
{
// Is File
DeleteFile( sPath );
}
}
} while( FindNextFile( hFind, &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, &fdFile ) ) == INVALID_HANDLE_VALUE )
{
MessageBox( NULL, L"", L"", MB_OK );
return false;
}
do
{
if( wcscmp( fdFile.cFileName, L"." ) != 0 &&
wcscmp( fdFile.cFileName, L".." ) != 0 )
{
wsprintf( sPath, L"%s\\%s", sDir, fdFile.cFileName );
if( fdFile.dwFileAttributes & 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, &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, &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 &&
wcscmp( fdFile.cFileName, L".." ) != 0 )
{
wsprintf( sPath, L"%s\\%s", sDir, fdFile.cFileName );
if( fdFile.dwFileAttributes & 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, &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;
}
我知道问题存在于我实际 RemoveDirectory(sPath);因为调用了该函数,所以它看到 ipch 不为空,因此不会删除它。然后递归并继续。
【问题讨论】:
标签: c++ winapi directory parent