Qt 文件夹操作

中文乱码问题:
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif

 打开文件夹
 QString dpath = QFileDialog::getExistingDirectory(this, "选择文件夹路径", "./");
 QDir dir(dpath);
 if (!dir.exists())
 {
	return;
 }
 dir.setFilter(QDir::Dirs  | QDir::NoDotAndDotDot);//文件夹|不包含./和../
//排序文件夹优先
 dir.setSorting(QDir::DirsFirst);
//获取文件夹下所有文件(文件夹+文件)
 QFileInfoList list = dir.entryInfoList();
 for (int i = 0; i < list.size(); i++) {
	QString dname = dpath;
	dname.append("/").append(list[i].fileName());
	QDir dd(dname);
	dd.setFilter(QDir::Files | QDir::NoDotAndDotDot);//文件夹|不包含./和../
	QFileInfoList li = dd.entryInfoList();
	qDebug() << "-----------------dpath:" << dname;
	for (int j = 0; j < li.size(); j++) {
		QString filepath = dname;
		filepath.append("/").append(li[j].fileName());
		QFileInfo info(filepath);
		QString strFormat = "yyyy_MM_dd_hh_mm_ss";
		QDateTime time = info.lastModified();
		QString strtime = time.toString(strFormat);
		QString oldname = li[j].fileName();
		QString newname = list[i].fileName();
		newname.append("_").append(strtime).append(".").append(oldname.split(".").takeLast());
		//qDebug() << "oldname:  " << oldname << " new name: " << newname;
		QFile file(filepath);
		//qDebug() << "file size: " << file.size();
		QString base = dname;
		base.append("/");
		QString old = base;
		old.append(oldname);
		QString ne = base;
		ne.append(newname);
		qDebug() << "old:  " << old << " ne: " << ne;
		bool flag = file.rename(ne);
		qDebug() << "filename: " << file.fileName() << " state: " << flag;
		}
	}

你可能感兴趣的:(Qt,qt)