Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Rename failed if path have 4 layers of subfolder but works if 2 layers involved.



  • I'm not sure what the problem. I just want to move files from different folder to others. But only worked if involve up until 2 subfolders. But as it overwrite the existing contents, I need to rename up until folder name that holding the files. But it's always return false. Thus, the file missing from original place but not the folders.
    The example of full path is "C:/Log/ORI/2021-08/2021-08-10/GUI01/20210707_000.log" to move to "C:/Log/ORI_Backup/2021-08/2021-08-10/GUI01/20210707_000.log"

    Any help is appreciated.

    QStringList BackUp::GetFilePathList(QString directory)
    {
    print_log("Path "+ directory, LOG_DEBUG);
    QStringList list;
        QDirIterator iterator (directory, QDir::Files | QDir::NoSymLinks, QDirIterator::Subdirectories);
        while(iterator.hasNext())
        {
            iterator.next();
            list << iterator.fileInfo().absoluteFilePath();
            print_log("Path "+ iterator.fileInfo().absoluteFilePath(), LOG_DEBUG);
        }
        return list;
    }
    
            QStringList filesPath = GetFilePathList(ORIPath);
            for(int i = 0; i < filesPath.size(); i++){
                QString oriPath = filesPath.at(i);
                QString newPath = filesPath.at(i);
                newPath = newPath.replace("ORI","/ORI_Backup");
                print_log("Filename to backup: "+ oriPath, LOG_DEBUG);
                print_log("Filename new destination: "+ newPath, LOG_DEBUG);
    
                if(QFile::exists(newPath)){
                    QFile::remove(newPath);
                }
    
                bool ret = QFile::copy(oriPath,newPath);
                //bool ret =  QFile::rename(oriPath,newPath);
                QFile::setPermissions(newPath, QFileDevice::ReadOwner|QFileDevice::WriteOwner);
                print_log("absoluteORI: "+ oriPath, LOG_DEBUG);
                print_log("absoluteNEW: "+ newPath, LOG_DEBUG);
                if(ret == true){
                    print_log(" SUCCESS copied" +newPath, LOG_INFO);
                }else{
                    print_log(" FAILED! " +newPath, LOG_ERROR);
                }
            }
    

  • Lifetime Qt Champion

    Works fine for me with Qt 5.15.2

    #include <QtWidgets>
    
    int main(int argc, char* argv[])
    {
        QApplication a(argc, argv);
        QString fromDir = "C:/Log/ORI/2021-08/2021-08-10/GUI01/";
        QString toDir = "C:/Log/ORI_Backup/2021-08/2021-08-10/GUI01/";
        QString fileName = "20210707_000.log";
        if (!QDir().exists(fromDir)) {
            QDir().mkpath(fromDir);
            QFile f(fromDir + fileName);
            f.open(QIODevice::WriteOnly);
            f.write(QByteArray(1024, '\1'));
            f.close();
        }
        if (!QDir().exists(toDir)) {
            QDir().mkpath(toDir);
        }
        else {
            QFile::remove(toDir + fileName);
        }
        if (QFile::rename(fromDir + fileName, toDir + fileName)) {
            QMessageBox::information(nullptr, "Rename", QString("Success, filesize: %1!").arg(QFileInfo(toDir + fileName).size()));
        }
        else {
            QMessageBox::warning(nullptr, "Rename", "Failed!");
        }
    
        QFile::remove(fromDir + fileName);
        QFile::remove(toDir + fileName);
        QDir("C:/Log").removeRecursively();
        return 0;
    }
    

  • Lifetime Qt Champion

    Works fine for me with Qt 5.15.2

    #include <QtWidgets>
    
    int main(int argc, char* argv[])
    {
        QApplication a(argc, argv);
        QString fromDir = "C:/Log/ORI/2021-08/2021-08-10/GUI01/";
        QString toDir = "C:/Log/ORI_Backup/2021-08/2021-08-10/GUI01/";
        QString fileName = "20210707_000.log";
        if (!QDir().exists(fromDir)) {
            QDir().mkpath(fromDir);
            QFile f(fromDir + fileName);
            f.open(QIODevice::WriteOnly);
            f.write(QByteArray(1024, '\1'));
            f.close();
        }
        if (!QDir().exists(toDir)) {
            QDir().mkpath(toDir);
        }
        else {
            QFile::remove(toDir + fileName);
        }
        if (QFile::rename(fromDir + fileName, toDir + fileName)) {
            QMessageBox::information(nullptr, "Rename", QString("Success, filesize: %1!").arg(QFileInfo(toDir + fileName).size()));
        }
        else {
            QMessageBox::warning(nullptr, "Rename", "Failed!");
        }
    
        QFile::remove(fromDir + fileName);
        QFile::remove(toDir + fileName);
        QDir("C:/Log").removeRecursively();
        return 0;
    }
    


  • Thanks, it's turn out some of the subfolder is not yet exists. Thank you @Christian-Ehrlicher


Log in to reply