风泥

导航

目录拷贝

目录拷贝

bool QtCopyDir(const QDir &fromDir, const QDir &toDir, bool bCoverIfFileExists)
{
    QDir formDir_ = fromDir;
    QDir toDir_ = toDir;

    if(!toDir_.exists())
    {
        if(!toDir_.mkdir(toDir.absolutePath()))
            return false;
    }

    QFileInfoList fileInfoList = formDir_.entryInfoList();
    foreach(QFileInfo fileInfo, fileInfoList)
    {
        if(fileInfo.fileName() == "." || fileInfo.fileName() == "..")
            continue;
        //copy child dir
        if(fileInfo.isDir())
        {
            //recursion call
            if(!QtCopyDir(fileInfo.filePath()
                                        , toDir_.filePath(fileInfo.fileName())))
            {
                return false;
            }
        }
        //copy child file
        else
        {
            if(bCoverIfFileExists && toDir_.exists(fileInfo.fileName()))
            {
                toDir_.remove(fileInfo.fileName());
            }
            if(!QFile::copy(fileInfo.filePath(), toDir_.filePath(fileInfo.fileName())))
            {
                return false;
            }
        }
    }
    return true;
}

posted on 2016-02-29 18:23  风泥  阅读(108)  评论(0)    收藏  举报