0

我在 windows 7 pro 上运行这段代码:

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();
        }
    }
}

对于我尝试移动的每个目录,重命名返回 false

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

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

有谁知道为什么它不起作用?我做错了什么 ?你有任何替代解决方案吗?

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

4

1 回答 1

1

只需使用该代码,在参数中使用 old_dir、new_dir 调用“moveNodeAndSubNodes”。此代码非常安全,如果 som 不会删除 orig dir

#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;
}
于 2013-02-28T10:26:07.170 回答