对不起,我之前提供的代码有误。以下是一个更新后的版本,用于将”5.3d.tiff”格式转换为”5_R.tiff”格式:
#include <QCoreApplication>
#include <QDir>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString path = "/path/to/directory"; // 替换为目标文件所在的路径
QDir directory(path);
QStringList fileList = directory.entryList(QStringList() << "*.tiff", QDir::Files);
foreach (QString file, fileList) {
QString oldName = directory.absoluteFilePath(file);
QString newName = oldName;
int dotIndex = newName.lastIndexOf(".");
if (dotIndex >= 0) {
QString prefix = newName.mid(0, dotIndex); // 提取前缀
if (prefix.contains(QRegExp("\\d+\\.\\d+d"))) { // 正则表达式匹配数字+点+数字+d格式的前缀
newName.replace(prefix, prefix.left(prefix.length() - 2) + "_R"); // 在前缀倒数第二个字符后添加"_R"
}
}
if (oldName != newName) {
if (QFile::rename(oldName, newName)) {
qDebug() << "Renamed:" << oldName << "->" << newName;
} else {
qDebug() << "Failed to rename:" << oldName;
}
}
}
return 0;
}
请确保将/path/to/directory
替换为实际存放这些文件的路径。该代码会遍历指定目录下所有以”.tiff”结尾的文件,并将满足 “数字.数字d” 格式的前缀转换为 “数字_R.tiff” 的命名格式。
内容由零声教学AI助手提供,问题来源于学员提问