递归删除文件夹

    windows API有一个RemoveDirectory(path)函数,但是必须是该文件夹为空,才能删除。在这个ActiveX中,因为只有3个需要另外存放的DLL,因此我是采用一下代码新建文件夹的:

  1.     TCHAR                   csStr[256];
  2.     CString                 sysDirPath;
  3.     GetSystemDirectory(csStr, 256);
  4.     sysDirPath = (CString)csStr + (CString)_T("//playerdll");
  5.     //在系统目录下创建playerdll文件夹,拷贝DLL,并注册
  6.     CreateDirectory(sysDirPath,NULL);
  7.     CString sourcefile=(CString)csStr+(CString)_T("//avformat-.dll");
  8.     CopyFile(sourcefile,sysDirPath+"//avformat.dll",FALSE);

    采用以下代码来删除文件的:

  1.     TCHAR                   csStr[256];
  2.     CString                 sysDirPath;
  3.     GetSystemDirectory(csStr, 256);
  4.     sysDirPath = (CString)csStr + (CString)_T("//playerdll");
  5.     DeleteFile(sysDirPath+(CString)_T("//avformat.dll"));
  6.     RemoveDirectory(sysDirPath);

    因为我们知道要拷入的文件名,因此可以使用上面的方法来解决。如果我们不知道一个文件夹中有哪些文件,那只能使用递归的方法来删除了。

 

  1. bool DeleteDirectory(char* sDirName) 
  2.     CFileFind tempFind; 
  3.     char sTempFileFind[200] ;
  4.     sprintf(sTempFileFind,"%s/*.*",sDirName); 
  5.     BOOL IsFinded = tempFind.FindFile(sTempFileFind); 
  6.     while (IsFinded) 
  7.     { 
  8.         IsFinded = tempFind.FindNextFile(); 
  9.         if (!tempFind.IsDots()) 
  10.     { 
  11.             char sFoundFileName[200]; 
  12.             strcpy(sFoundFileName,tempFind.GetFileName().GetBuffer(200)); 
  13.             
  14.             if (tempFind.IsDirectory()) 
  15.         { 
  16.                 char sTempDir[200]; 
  17.                 sprintf(sTempDir,"%s/%s",sDirName,sFoundFileName); 
  18.                 DeleteDirectory(sTempDir); 
  19.             } 
  20.             else 
  21.         { 
  22.                 char sTempFileName[200]; 
  23.                 sprintf(sTempFileName,"%s/%s",sDirName,sFoundFileName); 
  24.                 DeleteFile(sTempFileName); 
  25.             } 
  26.         } 
  27.     } 
  28.     tempFind.Close(); 
  29.     if(!RemoveDirectory(sDirName)) 
  30.     { 
  31.         return FALSE; 
  32.     } 
  33.     return TRUE; 
  34. BOOL DelAll()
  35. {
  36.     CString m_strDir="D://test";
  37.     if(PathFileExists(m_strDir))     
  38.         DeleteDirectory((LPSTR)(LPCTSTR)m_strDir);
  39.     return 1;
  40. }

注:sprintf用来实现连接字符串和格式化字符串。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值