现在的位置: 首页 > 综合 > 正文

Windows中删除文件夹

2013年10月07日 ⁄ 综合 ⁄ 共 951字 ⁄ 字号 评论关闭

#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();

抱歉!评论已关闭.