File.rename() takes in a literal string of directory, but not the variable?
Solved
C++ Gurus
-
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"; } } }
-
Never Mind, turns out there was a bug elsewhere in my code. Both methods work lol. I should really call it a day...
-