I'm trying to write a program that renames a certain list of files in a chosen directory with a new extension. Pretty much to replace all .dx90 files with .dx80 files. This is the code that I've written so far and it's not working. All the files are being placed into the failed files list but I'm getting no errors.
#include <QFileDialog>
#include <QString>
#include <QApplication>
#include <QDir>
#include <QStringList>
#include <QTextStream>
QTextStream cout(stdout);
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QString dirName = QFileDialog::getExistingDirectory(0, "Open Directory", QDir::currentPath(), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
QDir directory(dirName);
QStringList filters;
filters << "*.dx90";
QStringList files = directory.entryList(filters);
QStringList changedFiles, failedFiles;
foreach(QString filename, files)
{
QFileInfo info(filename);
QString rawFileName = filename.section(".", 0, 0);
QString newName = info.absoluteFilePath().section("/", 0, -2) + "/" + rawFileName + ".dx80";
bool success = directory.rename(info.absoluteFilePath(), newName);
if(success)
{
changedFiles << info.absoluteFilePath();
}
else
{
failedFiles << info.absoluteFilePath();
}
}
return 0;
}
I figured it out. The error I made was in the line:
QFileInfo info(filename);
It was not finding the file as the variable filename was not the absolute path. This made the info variable default to the QFileInfo of the current working directory of the application. I fixed the code by changing that line to:
QFileInfo info(directory.absolutePath() + "/" + filename);
Thanks to anyone that tried to help me fix the code. I hope this helps others that have similar problems.