C++ win32 api中的RemoveDirectory()函数不';t在删除子目录后删除父目录
在删除目录的子文件夹/文件后,删除目录时遇到问题。我正在使用FindFirstFile、FindTextFile、RemoveDirectory等。例如,我有以下目录: C:\Users\user\Desktop\SearchFile\ipch\SearchFile-e3798fd4\some-files-here.ipch 在我的程序运行后,剩下这个 C:\Users\user\Desktop\SearchFile\ipch 正如您所见,它删除了searchfile-e3798fd4中的所有文件并删除了目录,但保留了父目录“ipch”,我也希望删除它。这是我当前的完整代码:C++ win32 api中的RemoveDirectory()函数不';t在删除子目录后删除父目录,c++,winapi,directory,parent,C++,Winapi,Directory,Parent,在删除目录的子文件夹/文件后,删除目录时遇到问题。我正在使用FindFirstFile、FindTextFile、RemoveDirectory等。例如,我有以下目录: C:\Users\user\Desktop\SearchFile\ipch\SearchFile-e3798fd4\some-files-here.ipch 在我的程序运行后,剩下这个 C:\Users\user\Desktop\SearchFile\ipch 正如您所见,它删除了searchfile-e3798fd4中的所有文
#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;
}
#包括
#包括
#包括
布尔删除目录(常量wchar\u t*sDir)
{
WIN32_FIND_数据文件;
处理高频风;
wchar_t sPath[最大路径*10];
wsprintf(sPath,L“%s\\*.*”,sDir);
if((hFind=FindFirstFile(sPath,&fdFile))==无效的句柄值)
{
返回false;
}
做
{
如果(wcscmp(fdFile.cFileName,L.“)!=0&&
wcscmp(fdFile.cFileName,L.“.”!=0)
{
wsprintf(sPath,L“%s\\%s”,sDir,fdFile.cFileName);
if(fdFile.dwFileAttributes和文件属性目录)
{
//是目录
罢免董事(sPath);
删除目录(sPath);
}
}
}while(FindNextFile(hFind,&fdFile));
FindClose(hFind);
返回true;
}
bool DeleteFiles(const wchar\u t*sDir、WIN32\u FIND\u DATA fdFile、HANDLE hFind)
{
wchar_t sPath[最大路径*10];
wsprintf(sPath,L“%s\\*.*”,sDir);
if((hFind=FindFirstFile(sPath,&fdFile))==无效的句柄值)
{
消息框(空,L“123”,L“123”,MB_OK);
返回false;
}
做
{
如果(wcscmp(fdFile.cFileName,L.“)!=0&&
wcscmp(fdFile.cFileName,L.“.”!=0)
{
wsprintf(sPath,L“%s\\%s”,sDir,fdFile.cFileName);
if(fdFile.dwFileAttributes和文件属性目录)
{
//是目录
删除文件(sPath、fdFile、hFind);
}
其他的
{
//是文件
删除文件(sPath);
}
}
}while(FindNextFile(hFind,&fdFile));
FindClose(hFind);
返回true;
}
bool DeleteFolderContents(const wchar_t*sDir)
{
WIN32_FIND_数据文件;
句柄hFind=NULL;
wchar_t sPath[最大路径*10];
wsprintf(sPath,L“%s\\*.*”,sDir);
if((hFind=FindFirstFile(sPath,&fdFile))==无效的句柄值)
{
消息框(空,L“”,L“”,MB_OK);
返回false;
}
做
{
如果(wcscmp(fdFile.cFileName,L.“)!=0&&
wcscmp(fdFile.cFileName,L.“.”!=0)
{
wsprintf(sPath,L“%s\\%s”,sDir,fdFile.cFileName);
if(fdFile.dwFileAttributes和文件属性目录)
{
//是目录
如果(wcscmp(fdFile.cFileName,L“Debug”)==0||
wcscmp(fdFile.cFileName,L“Release”)==0||
wcscmp(fdFile.cFileName,L“ipch”)==0)
{
删除文件(sPath、fdFile、hFind);
}
删除文件夹内容(sPath);
}
}
}while(FindNextFile(hFind,&fdFile));
FindClose(hFind);
返回true;
}
bool ListDirectoryContents(常量wchar\u t*sDir)
{
WIN32_FIND_数据文件;
句柄hFind=NULL;
wchar_t sPath[最大路径*10];
wsprintf(sPath,L“%s\\*.*”,sDir);
if((hFind=FindFirstFile(sPath,&fdFile))==无效的句柄值)
{
消息框(NULL,L“未找到路径”,L“错误”,MB|U OK | MB|U ICONERROR);
返回false;
}
做
{
如果(wcscmp(fdFile.cFileName,L.“)!=0&&
wcscmp(fdFile.cFileName,L.“.”!=0)
{
wsprintf(sPath,L“%s\\%s”,sDir,fdFile.cFileName);
if(fdFile.dwFileAttributes和文件属性目录)
{
//是目录
如果(wcscmp(fdFile.cFileName,L“Debug”)==0||
wcscmp(fdFile.cFileName,L“Release”)==0||
wcscmp(fdFile.cFileName,L“ipch”)==0)
{
//在调试文件夹中工作
wprintf(L“[+]%s\n”,sPath);
}
ListDirectoryContents(sPath);
}
其他的
{
//是文件
wprintf(L“[-]%s\n”,sPath);
}
}
}while(FindNextFile(hFind,&fdFile));
FindClose(hFind);
返回true;
}
int main()
{
wchar\u t路径名[]=L“C:\\Users\\user\\Desktop\\SearcFile\\”;
//列出目录内容
ListDirectoryContents(路径名);
wprintf(L“\n”);
系统(“暂停”);
//删除文件夹内容
DeleteFolderContents(路径名);
删除目录(路径名);
wprintf(L“\n”);
系统(“暂停”);
ListDirectoryContents(路径名);
wprintf(L“\n”);
系统(“暂停”);
返回0;
}
我知道问题存在于我实际移除的地方(sPath);因为调用了该函数,所以它看到ipch不是空的,因此不会删除它。然后递归并继续。您的递归有三个问题,与Tim描述的算法不匹配:
RemoveDirectory
只删除空目录removeddirectory
调用从循环内部移动到函数末尾将解决这些问题
} while( FindNextFile( hFind, &fdFile ) );
::RemoveDirectory (sDir) ; // Add this here.
FindClose( hFind );
return true;
#include <string>
#include <Windows.h>
#define DIR L"C:\\Users\\user\\Desktop\\SearchFile\\ipch"
BOOL RmDir (const std::wstring &strDir) ;
BOOL IsDots (const std::wstring &strName) ;
BOOL IsDir (const WIN32_FIND_DATA &fdFile) ;
int main (void)
{
if (RmDir (DIR) == FALSE) {
return 1 ;
}
return 0 ;
}
BOOL RmDir (const std::wstring &strDir)
{
WIN32_FIND_DATA fdFile ;
::memset (&fdFile, 0, sizeof (fdFile)) ;
HANDLE hFind = INVALID_HANDLE_VALUE ;
std::wstring strSearch = strDir + L"\\*.*" ;
hFind = ::FindFirstFile (strSearch.data (), &fdFile) ;
if (hFind == INVALID_HANDLE_VALUE) {
return FALSE ;
}
do {
std::wstring strDelete = strDir + L"\\" + fdFile.cFileName ;
if (IsDir (fdFile) == TRUE) {
if (IsDots (fdFile.cFileName) == TRUE) {
continue ;
}
RmDir (strDelete) ;
}
else {
::DeleteFile (strDelete.data ()) ;
}
} while (::FindNextFile (hFind, &fdFile) == TRUE) ;
::FindClose (hFind) ;
::RemoveDirectory (strDir.data ()) ;
return TRUE ;
}
BOOL IsDots (const std::wstring &strName)
{
return strName == L"." || strName == L".." ;
}
BOOL IsDir (const WIN32_FIND_DATA &fdFile)
{
return (fdFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0 ;
}