zoukankan      html  css  js  c++  java
  • 【转】VC MFC 如何删除文件,目录,文件夹

    原文网址:http://shijuanfeng.blogbus.com/logs/100675115.html

    第一种方法:定义一个文件类对象来操作
    CFile   TempFile;   
      TempFile.Remove(指定文件名); 
    第二种方法:  使用系统函数 DeleteFile( LPCSTR filename )删除文件    _rmdir(),删除目录 DeleteDirectory(sTempDir);  删除目录 RemoveDirectory(sTempDir);删除目录
    eg:  DeleteFile(   char   *tempFileName);   

    令注:若要清空文件,但保留目录用: system(“del  C: emp”); // 清空了C: emp中的所有文件,但是不会清楚文件夹下的子目录,而且会弹出:是否删除的Doc框 

    //删除文件夹目录(非空) 上面提到的删除目录的方法只能删除空目录(即文件夹),如果目录下有文件或者子目录,就不能删除了,VC里好像没有直接的函数,只能手动写个函数来删除了,下面提供一个删除非空目录的方法: 

    bool DeleteDirectory(char* sDirName)
    {

    CFileFind tempFind;
      char sTempFileFind[200];
      strcpy(sTempFileFind, sDirName);
      strcat(sTempFileFind, “\*.*”);
     
      BOOL IsFinded = tempFind.FindFile(sTempFileFind);
      while (IsFinded)
      {
       IsFinded = tempFind.FindNextFile();  
      
       char sFoundFileName[200];
       strcpy(sFoundFileName,tempFind.GetFilePath()); 
       DeleteFile(sFoundFileName);   
     }
      tempFind.Close();

    }

    清空整个文件夹的内容(包括子文件夹),但保留该文件夹

    void CRelCtrlDlg::DeleteDirectory(char* sDirName)
    {
     char sPath[200];
     strcpy(sPath, sDirName);

     CFileFind   ff;
     BOOL   bFound;
     char sTempFileFind[200];
     strcpy(sTempFileFind, sPath);
     strcat(sTempFileFind, “\*.*”);

     bFound   =   ff.FindFile(sTempFileFind);

     while(bFound)
     {
      bFound   =   ff.FindNextFile();
      CString  sFilePath   =   ff.GetFilePath();

      if(ff.IsDirectory())
      {
       if(!ff.IsDots())
        DeleteDirectory((LPSTR)(LPCTSTR)sFilePath);
      }
      else
      {
       if(ff.IsReadOnly())
       {
        SetFileAttributes(sFilePath,   FILE_ATTRIBUTE_NORMAL);
       }
       DeleteFile(sFilePath);
      }
     }
     ff.Close();
     SetFileAttributes(sPath,   FILE_ATTRIBUTE_NORMAL);
     if (!strcmp(sPath,sDirName))
     {
      return;
     }
     RemoveDirectory(sPath);
    }

  • 相关阅读:
    逗号表达式
    Windows UninstallTool(右键卸载工具) 方便、快捷卸载电脑中的软件
    获取 Python 模块的路径
    Py2exe 打包后图标不显示[转载]
    获取系统文件关联图标
    py2exe 打包的exe,添加管理员权限
    获取注册表某键下的所有子键
    [已解决]Python脚本运行出错:libs\chardet\universaldetector.py:90: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode
    Git使用
    SQL Server获取指定行(如第二行)的数据
  • 原文地址:https://www.cnblogs.com/wi100sh/p/4319501.html
Copyright © 2011-2022 走看看