Domanda

I'm running this code on 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();
        }
    }
}

For every directories I try to move, rename return false

I check : The old path exist, the new path is created. I have enough rights on both directories.

The directorie is on another server (it start with "\\"). It work to copy to that directorie from anywhere (even from a completely different server)

Anyone know why it doesn't work ? What did I do wrong ? Do you have any alternative solution ?

EDIT : For mysterious reason, it doesn't make the toPath anymore

È stato utile?

Soluzione

just use that code, call 'moveNodeAndSubNodes' with old_dir, new_dir in params. This code is quite safe, and doesn't remove orig dir if som

#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;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top