首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >QDir::rename()不起作用

QDir::rename()不起作用
EN

Stack Overflow用户
提问于 2013-02-28 16:31:30
回答 1查看 2.1K关注 0票数 0

我在Windows7专业版上运行这段代码:

代码语言:javascript
复制
foreach(QString str, directorie.entryList(QStringList(), QDir::Dirs))
{
    if(str != "." && str != "..")
    {
        QDir path(directorie.path() + "\\" + str + "\\" + from.path());
        if(path.exists())
        {
            QDir toPath(directorie.path() + "\\" + str + "\\" + to.path() + "\\" + path.dirName());
            QDir make(directorie.path() + "\\" + str);
            qDebug() << make.mkpath(to.path() + "\\" + path.dirName());
            QDir dir;
            qDebug() << dir.rename(path.path(), toPath.path()) << path.path() << toPath.path();
        }
    }
}

对于我尝试移动的每个目录,rename return false

我检查:旧路径存在,新路径创建。我对这两个目录都有足够的权限。

目录在另一台服务器上(它以“\”开头)。它可以从任何地方(甚至从完全不同服务器)复制到目录

有人知道它为什么不起作用吗?我做错了什么?你有其他的解决方案吗?

编辑:由于神秘的原因,它不再使toPath

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-28 18:26:07

只需使用代码,在参数中使用old_dir和new_dir调用'moveNodeAndSubNodes‘即可。这段代码非常安全,并且不会删除原始目录

代码语言:javascript
复制
#include <QDir>
#include <QDebug>
#include <QString>
#include <QDateTime>
#include <QFileInfoList>
#include <QFileInfo>

bool moveNodeAndSubNodes(QString from, QString to);
void moveDir(QString from, QString to);
QStringList findFiles(QString dir);

void moveDir(QString from, QString to)
{
    qDebug() << "from=" << from << "to=" << to;

    QDir source_dir(from);
    if (source_dir.exists()) {

        QDir dest_dir(to);
        if (!dest_dir.exists()) {
            qDebug() << "dest dir doesn't exist, create it" << to;
            dest_dir.mkpath(".");
        }

        foreach (QFileInfo info, source_dir.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot)) {

            QString old_path = info.absoluteFilePath();
            QString new_path = QString("%1/%2").arg(to).arg(info.fileName());

            if (info.isDir()) {
                // recreate dir
                qDebug() << "move dir" << old_path << "to" << new_path;
                moveDir(old_path, new_path);
            }
            else {
                // recreate file
                qDebug() << "move file" << old_path << "to" << new_path;
                QFile::rename(old_path, new_path);
            }
        }
    }
    else { qDebug() << "error : source dir doesn't exist :" << from; }
}

QStringList findFiles(QString dir)
{
    QStringList ret;
    QDir source_dir(dir);
    if (source_dir.exists()) {
        foreach (QFileInfo info, source_dir.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot)) {
            if (info.isDir()) {
                ret << findFiles(info.absoluteFilePath());
            }
            else {
                ret << info.absoluteFilePath();
            }
        }
    }
    return ret;
}

bool moveNodeAndSubNodes(QString from, QString to)
{
    bool ok  = false;
    moveDir(from, to);
    QStringList files = findFiles(from);
    qDebug() << "files not deleted =" << files;
    if (files.isEmpty()) {
        QDir rm_dir(from);
        ok = rm_dir.removeRecursively();
        qDebug() << "source dir removed =" << ok;
    }
    else {
        qDebug() << "source dir not empty, not removed";
    }
    return ok;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15130840

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档