Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. File.rename() takes in a literal string of directory, but not the variable?
Forum Updated to NodeBB v4.3 + New Features

File.rename() takes in a literal string of directory, but not the variable?

Scheduled Pinned Locked Moved Solved C++ Gurus
2 Posts 1 Posters 320 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jay_emissary
    wrote on last edited by
    #1

    Hello everyone, I'm trying to dynamically rename files that I pass in via QML, but can't quite figure out why the "rename" method isn't taking in the newFilePath variable. NewFilePath literally returns a string with the exact same directory as "C:/Users/yyyy/Downloads/FileSorter/build/Desktop_Qt_6_7_2_MinGW_64_bit-Debug/twcs).txt/New Text Document.wav", but with a different file name, so how come rename fails every time?

    Fails

    void FileDataHandler::editFile(QUrl folderDirectory, int index, QString fileName){
    
    
        QDir dir(folderDirectory.toLocalFile()); //removes the file:/// part of the url so that the path is now valid
        QFile currentFile(dir.absoluteFilePath(m_fileList[index])); //creating the reference (absolutePath returns a folder directory)
        QString newFilePath = dir.absolutePath() + "/" + fileName + m_extensionType;
        //qInfo() << currentFile.exists();
        if(m_files && m_fileList.size() > 0)
        {
            
            qInfo() << fileName;
            qInfo() << dir.absolutePath() + "/" + fileName + m_extensionType;
            qInfo() << newFilePath;
            qInfo() << "C:/Users/yyyy/Downloads/FileSorter/build/Desktop_Qt_6_7_2_MinGW_64_bit-Debug/twcs).txt/New Text Document.wav";
          
            if(currentFile.rename(newFilePath))
           {
    
               qInfo() << currentFile.fileName();
                    qInfo() << dir.absoluteFilePath(m_fileList[index]);
           }
           else{
    
    
               qInfo() << newFilePath << "FAIL";
           }
        }
    
    }
    
    

    Works

    void FileDataHandler::editFile(QUrl folderDirectory, int index, QString fileName){
    
    
        QDir dir(folderDirectory.toLocalFile()); //removes the file:/// part of the url so that the path is now valid
        QFile currentFile(dir.absoluteFilePath(m_fileList[index])); //creating the reference (absolutePath returns a folder directory)
        QString newFilePath = dir.absolutePath() + "/" + fileName + m_extensionType;
        //qInfo() << currentFile.exists();
        if(m_files && m_fileList.size() > 0)
        {
            
            qInfo() << fileName;
            qInfo() << dir.absolutePath() + "/" + fileName + m_extensionType;
            qInfo() << newFilePath;
            qInfo() << "C:/Users/yyyy/Downloads/FileSorter/build/Desktop_Qt_6_7_2_MinGW_64_bit-Debug/twcs).txt/New Text Document.wav";
          
            if(currentFile.rename("C:/Users/yyyy/Downloads/FileSorter/build/Desktop_Qt_6_7_2_MinGW_64_bit-Debug/twcs).txt/New Text Document.wav"))
           {
    
               qInfo() << currentFile.fileName();
                    qInfo() << dir.absoluteFilePath(m_fileList[index]);
           }
           else{
    
    
               qInfo() << newFilePath << "FAIL";
           }
        }
    
    }
    
    1 Reply Last reply
    0
    • J Offline
      J Offline
      Jay_emissary
      wrote on last edited by
      #2

      Never Mind, turns out there was a bug elsewhere in my code. Both methods work lol. I should really call it a day...

      1 Reply Last reply
      0
      • J Jay_emissary has marked this topic as solved on

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved