2013-02-28 1 views
0

작동하지 않습니다 내가 이동하려고 모든 디렉토리에 대한QDir :: 이름 바꾸기() 나는 프로 윈도우 7에이 코드를 실행하는거야

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를 반환 이름을 변경하십시오 이전 경로가 있으면 새 경로가 만들어집니다. 두 디렉터리에 충분한 권한이 있습니다.

디렉터는 다른 서버에 있습니다 ("\\"로 시작). 어디서나 (완전히 다른 서버에서도) 해당 디렉터에게 복사 할 수 있습니다.

누구나 왜 작동하지 않는지 알 수 있습니까? 나는 무엇을 잘못 했는가? 다른 해결책이 있습니까?

편집 : 신비 이유로, 그것은 더 이상

+0

BSOD는 이와 같이 Qt 코드를 실행하면 안됩니다. 나쁜 HW 또는 운전자 문제가있는 것 같습니다. – drescherjm

+0

bluscreen은 아마도 다른 것 때문일 것이므로 더 이상 필요하지 않으므로 잊어 버리자. – BlueMagma

답변

1

그냥 코드를 사용하는 toPath을 old_dir와 'moveNodeAndSubNodes', 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; 
}