zoukankan      html  css  js  c++  java
  • C++ Delete Directory and File

    Method 1:Using system Command

    #include< stdlib.h > 
    #include< stdio.h > 
    
    int main( ) 
    { 
    system( "md d://aa//zhao " ); // 在下新建文件夹 
    system( "del d://aa//zhao " ); // 删除该文件夹下的所有文件 
    }


    Method 2:Using DeleteDirectory Function, you should delete files in the directory first.

    BOOL CDlgData::DeleteDirectory(char *sDirName)
    {
        CFileFind tempFind; 
        char sTempFileFind[200] ;
     
        sprintf(sTempFileFind,"%s\\*.*",sDirName); 
        BOOL IsFinded = tempFind.FindFile(sTempFileFind); 
        while (IsFinded) 
        { 
            IsFinded = tempFind.FindNextFile(); 
     
            if (!tempFind.IsDots()) 
            { 
                char sFoundFileName[200]; 
                strcpy(sFoundFileName,tempFind.GetFileName().GetBuffer(200)); 
     
                if (tempFind.IsDirectory()) 
                { 
                    char sTempDir[200]; 
                    sprintf(sTempDir,"%s\\%s",sDirName,sFoundFileName); 
                    DeleteDirectory(sTempDir); 
                } 
                else 
                { 
                    char sTempFileName[200]; 
                    sprintf(sTempFileName,"%s\\%s",sDirName,sFoundFileName); 
                    DeleteFile(sTempFileName); 
                } 
            } 
        } 
        tempFind.Close(); 
        if(!RemoveDirectory(sDirName)) 
        { 
            return FALSE; 
        } 
        return TRUE; 
    }

    Method 3:Using SHFileOperation API

    Function Prototype:

    WINSHELLAPI int WINAPI SHFileOperation(
        LPSHFILEOPSTRUCT lpFileOp 
       );函数删除包含文件的目录

    //删除文件夹
    SHFILEOPSTRUCT	 FileOp;   
    FileOp.fFlags	=	FOF_NOCONFIRMATION;   
    FileOp.hNameMappings   =   NULL;   
    FileOp.hwnd   =   NULL;   
    FileOp.lpszProgressTitle   =   NULL;   
    FileOp.pFrom	=   "要删除的目录";   
    FileOp.pTo	 =	 NULL;	 
    FileOp.wFunc	=   FO_DELETE;	
    SHFileOperation(&FileOp);


  • 相关阅读:
    OSI结构和TCP/IP模型
    将[4,3,2,5,4,3]分割成[4,3,2]、[5,4,3]两个List的算法
    Mybatis增加对象属性不增加mapper.xml的情况
    以脚本方式直接执行修改密码的passwd命令
    Raphael的鼠标over move out事件
    Raphael的Braille例子
    Raphael的set使用
    Raphael的transform用法
    Raphael的text及对齐方式
    Raphael初始化,path,circle,rect,ellipse,image
  • 原文地址:https://www.cnblogs.com/yefengmeander/p/2887556.html
Copyright © 2011-2022 走看看