创建删除多级目录


//创建多级目录
BOOL MakeDirectory(CString dd)
{
    HANDLE        fFile;        // File Handle
    WIN32_FIND_DATA    fileinfo;    // File Information Structure
    CStringArray    m_arr;        // CString Array to hold Directory Structures
    BOOL tt;            // BOOL used to test if Create Directory was successful
    int x1 = 0;            // Counter
    CString tem = "";        // Temporary CString Object
 
    fFile = FindFirstFile(dd,&fileinfo);
 
    // if the file exists and it is a directory
    if(fileinfo.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
    {
        //  Directory Exists close file and return
        FindClose(fFile);
        return TRUE;
    }
    m_arr.RemoveAll();
 
    for(x1 = 0; x1 < dd.GetLength(); x1++ )        // Parse the supplied CString Directory String
    {                                    
        if(dd.GetAt(x1) != '//')        // if the Charachter is not a /
            tem += dd.GetAt(x1);        // add the character to the Temp String
        else
        {
            m_arr.Add(tem);            // if the Character is a /
            tem += "//";            // Now add the / to the temp string
        }
        if(x1 == dd.GetLength()-1)        // If we reached the end of the String
            m_arr.Add(tem);
    }
 
    // Close the file
    FindClose(fFile);
 
    // Now lets cycle through the String Array and create each directory in turn
    for(x1 = 1; x1 < m_arr.GetSize(); x1++)
    {
        tem = m_arr.GetAt(x1);
        tt = CreateDirectory(tem,NULL);
 
        // If the Directory exists it will return a false
        if(tt)
            SetFileAttributes(tem,FILE_ATTRIBUTE_NORMAL);
        // If we were successful we set the attributes to normal
    }
    //  Now lets see if the directory was successfully created
    fFile = FindFirstFile(dd,&fileinfo);
 
    m_arr.RemoveAll();
    if(fileinfo.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
    {
        //  Directory Exists close file and return
        FindClose(fFile);
        return TRUE;
    }
    else
    {
        // For Some reason the Function Failed  Return FALSE
        FindClose(fFile);
        return FALSE;
    }
}

//删除多级目录
BOOL DeleteDirectory(char *DirName)//如删除 DeleteDirectory("c://aaa")
{
    CFileFind tempFind;
    char tempFileFind[MAX_PATH];
    sprintf(tempFileFind,"%s//*.*",DirName);
    BOOL IsFinded=(BOOL)tempFind.FindFile(tempFileFind);
    while(IsFinded)
    {
        IsFinded=(BOOL)tempFind.FindNextFile();
        if(!tempFind.IsDots())
        {
            char foundFileName[MAX_PATH];
            strcpy(foundFileName,tempFind.GetFileName().GetBuffer(MAX_PATH));
            if(tempFind.IsDirectory())
            {
                char tempDir[MAX_PATH];
                sprintf(tempDir,"%s//%s",DirName,foundFileName);
                DeleteDirectory(tempDir);
            }
            else
            {
                char tempFileName[MAX_PATH];
                sprintf(tempFileName,"%s//%s",DirName,foundFileName);
                DeleteFile(tempFileName);
            }
        }
    }
    tempFind.Close();
    if(!RemoveDirectory(DirName))
    {
        MessageBox(0,"删除目录失败!","警告信息",MB_OK);//比如没有找到文件夹,删除失败,可把此句删除
        return FALSE;
    }
    return TRUE;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值