gpt4 book ai didi

c++ - win32 api 中的 RemoveDirectory() 函数在删除子目录后不会删除父目录

转载 作者:行者123 更新时间:2023-11-30 04:08:24 26 4
gpt4 key购买 nike

<分区>

我在删除子文件夹/文件后删除目录时遇到问题。我正在使用 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 不为空,因此不会删除它。然后递归并继续。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com