Windows中删除文件夹

    技术2022-05-11  68

    #include <iostream>#include <string.h>#include <windows.h>

    using namespace std;

    BOOL DelDir(char *szDir){ WIN32_FIND_DATA FindFileData; HANDLE hFind; char szDirName[300] = "/0"; char szFileName[300] = "/0";

     sprintf(szDirName, "%s//*.*", szDir); hFind = FindFirstFile(szDirName, &FindFileData);

     if (hFind == INVALID_HANDLE_VALUE) {  return FALSE; }

     do {  if ( (strcmp(FindFileData.cFileName, ".") != 0) && (strcmp(FindFileData.cFileName, "..") != 0) )  {   sprintf(szFileName, "%s//%s", szDir, FindFileData.cFileName);   DWORD dwAttr = GetFileAttributes(szFileName);

       if (dwAttr & FILE_ATTRIBUTE_DIRECTORY)   {    DelDir(szFileName);   }   else   {    DeleteFile(szFileName);     }  }

     } while(FindNextFile(hFind, &FindFileData));  FindClose(hFind);

     char szTrace[32]; BOOL bRet = RemoveDirectory(szDir); if (!bRet) {  sprintf(szTrace, "%lu - ", GetLastError());  OutputDebugString(szTrace); }

     OutputDebugString(szDir); OutputDebugString("/n");  return true;}

    int main(){ DelDir("D://Test//testdel"); getchar();} 


    最新回复(0)