2013-02-28 116 views
0

我跑的親Windows 7的驗證碼的QDir ::重命名()不工作

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

我檢查:舊路徑存在,新路徑被創建。 我對這兩個目錄都有足夠的權利。

directorie位於另一臺服務器上(以「\\」開頭)。它可以從任何地方複製到該導演(甚至從一個完全不同的服務器)

任何人都知道爲什麼它不起作用?我做錯了什麼 ?你有其他解決方案嗎?

編輯:對於神祕的原因,它不會使toPath了

+0

不應該由執行這樣的Qt代碼引起藍屏死機。這聽起來像你有不好的硬件或驅動程序問題。 – drescherjm 2013-02-28 09:29:05

+0

bluscreen可能是由於別的東西,我沒有了,所以讓我們忘了它 – BlueMagma 2013-02-28 09:45:06

回答

1

只是使用這些代碼,稱之爲 'moveNodeAndSubNodes' 與old_dir,NEW_DIR params中。此代碼是相當安全的,並且不刪除原始目錄如果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; 
}