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

创建删除多级目录

2013年09月18日 ⁄ 综合 ⁄ 共 2852字 ⁄ 字号 评论关闭


//创建多级目录
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;
}

 

抱歉!评论已关闭.