Qt删除文件夹,递归寻找文件,选择文件所在文件夹

1.递归删除文件夹,包括空文件夹

bool DeleteFileOrFolder(const QString& strPath)
{
    if (strPath.isEmpty() || !QDir().exists(strPath))//是否传入了空的路径||路径是否存在
        return false;

    QFileInfo FileInfo(strPath);

    if (FileInfo.isFile())//如果是文件
        QFile::remove(strPath);
    else if (FileInfo.isDir())//如果是文件夹
    {
        QDir qDir(strPath);
        qDir.removeRecursively();
    }
    return true;
}

 2.指定目录递归寻找文件夹

//寻找到文件返回0,找不到返回非零值
int FindFileFromDir(const QString& _filePath, const QString& fileName)
{
    QDir dir(_filePath);   //QDir的路径一定要是全路径,相对路径会有错误

    if (!dir.exists())
        return -1;

    //取到所有的文件和文件名,去掉.和..文件夹
    dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
    dir.setSorting(QDir::DirsFirst);
    //将其转化为一个list
    QFileInfoList list = dir.entryInfoList();
    if (list.size() < 1)
        return -1;

    int i = 0;
    //采用递归算法
    do {
        QFileInfo fileInfo = list.at(i);
        bool bisDir = fileInfo.isDir();
        if (bisDir) {
            int ret = FindFileFromDir(fileInfo.filePath(), fileName);
            if (0 == ret) {
                return 0;
            }
        }
        else {
            if (0 == fileInfo.fileName().compare(fileName, Qt::CaseInsensitive)) {
                qDebug() << "has find " << fileInfo.filePath() << ":" << fileInfo.fileName();
                return 0;
            }
        }

        ++i;
    } while (i < list.size());

    return -1;
}

 3.检查文件是否被占用

bool isFileUsed(QString path)
{
    bool isUsed = false;

    QString pathx = path + "x";
    QFileInfo fileInfo(path);
    if (fileInfo.isDir()) {
        QDir dir(path);
        if (dir.exists()) {
            bool isCanRename = dir.rename(path, pathx);
            if (isCanRename == false) {
                isUsed = false;
            }
            else {
                dir.rename(pathx, path);
            }
        }

    }
    else if (fileInfo.isFile()) {
        QFile file(path);
        if (file.exists())
        {
            bool isCanRename = file.rename(path, pathx);
            if (isCanRename == false) {
                isUsed = true;
            }
            else {
                file.rename(pathx, path);
            }
        }
        file.close();
    }

    return isUsed;
}

4.寻找文件所在的文件夹

QString findDirByFileName(const QString& strDir, const QString& fileName)
{
    QDir dir(strDir);   //QDir的路径一定要是全路径,相对路径会有错误

    if (!dir.exists())
        return QString();

    //取到所有的文件和文件名,去掉.和..文件夹
    dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
    dir.setSorting(QDir::DirsFirst);
    //将其转化为一个list
    QFileInfoList list = dir.entryInfoList();
    if (list.size() < 1)
        return QString();

    int i = 0;
    //采用递归算法
    do {
        QFileInfo fileInfo = list.at(i);
        bool bisDir = fileInfo.isDir();
        if (bisDir) {
            QString strDir = findDirByFileName(fileInfo.filePath(), fileName);
            if (!strDir.isEmpty()) {
                return strDir;
            }
        }
        else {
            if (0 == fileInfo.fileName().compare(fileName, Qt::CaseInsensitive)) {
                qDebug() <<"has find " << fileInfo.filePath() << ":" << fileInfo.fileName();
                QString mdir = fileInfo.absoluteDir().absolutePath();
                return mdir;
            }
        }

        ++i;
    } while (i < list.size());

    return QString();
}

5.拷贝文件夹


bool copyFolder(const QString& fromDir, const QString& toDir, bool coverFileIfExist)
{
    QDir sourceDir(fromDir);
    QDir targetDir(toDir);

    if (!targetDir.exists())
    {    //如果目标目录不存在,则进行创建 
        if (!targetDir.mkdir(targetDir.absolutePath())) return false;
    }

    QFileInfoList fileInfoList = sourceDir.entryInfoList();
    foreach(QFileInfo fileInfo, fileInfoList)
    {
        if (fileInfo.fileName() == "." || fileInfo.fileName() == "..") continue;

        if (fileInfo.isDir())
        {    // 当为目录时,递归的进行copy 
            if (!copyFolder(fileInfo.filePath(),
                targetDir.filePath(fileInfo.fileName()),
                coverFileIfExist))
                return false;
        }
        else
        {   //当允许覆盖操作时,将旧文件进行删除操作
            if (coverFileIfExist && targetDir.exists(fileInfo.fileName()))
            {
                targetDir.remove(fileInfo.fileName());
            }

            // 进行文件拷贝
            if (!QFile::copy(fileInfo.filePath(), targetDir.filePath(fileInfo.fileName())))
            {
                return false;
            }
        }
    }
    return true;
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值