当前位置: 首页 > ops >正文

常见的文件夹操作(附源码)

以下是一些常见的文件夹操作:清空文件夹、获取文件夹最新文件、获取文件夹第二新文件、拷贝文件夹

1、清空文件夹

/*删除文件*/
void clearFolder(const QString &folderPath) 
{QDir dir(folderPath);// 检查文件夹是否存在if (!dir.exists()) {qWarning() << "Folder does not exist:" << folderPath;return;}/*筛选清空的文件类别*/QStringList list;list << "*.png";dir.setNameFilters(list);// 遍历文件夹中的所有文件和子文件夹QFileInfoList fileList = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::Files | QDir::Dirs);for (const QFileInfo &fileInfo : fileList) {if (fileInfo.isDir()) {// 递归删除子文件夹clearFolder(fileInfo.absoluteFilePath());dir.rmdir(fileInfo.fileName());}else {// 删除文件QFile::remove(fileInfo.absoluteFilePath());}}
}

2、获取文件夹最新文件

/*获取文件夹中最新的文件*/
QString getNewestFile(const QString &folderPath)
{QDir dir(folderPath);// 检查文件夹是否存在if (!dir.exists()) {qWarning() << "Folder does not exist:" << folderPath;return QString();}// 获取文件夹中的所有文件QFileInfoList fileList = dir.entryInfoList(QDir::Files);/*筛选查找的文件类别*/QStringList list;list << "*.png";dir.setNameFilters(list);if (fileList.isEmpty()) {qWarning() << "No files found in folder:" << folderPath;return QString();}// 初始化最新文件和时间QFileInfo newestFile = fileList.first();QDateTime newestTime = newestFile.lastModified();// 遍历文件列表,找到最新的文件for (const QFileInfo &fileInfo : fileList) {QDateTime fileTime = fileInfo.lastModified();if (fileTime > newestTime) {newestTime = fileTime;newestFile = fileInfo;}}// 返回最新文件的绝对路径return newestFile.absoluteFilePath();
}

3、获取文件夹第二新文件

/*获取文件夹中最二新的文件*/
QString getSecondNewestFile(const QString &folderPath)
{QDir dir(folderPath);if (!dir.exists()) {qWarning() << "Folder does not exist:" << folderPath;return QString();}/*筛选查找的文件类型*/QStringList list;list << "*.png";dir.setNameFilters(list);// 获取文件夹下所有文件QFileInfoList fileList = dir.entryInfoList(QDir::Files, QDir::Time | QDir::Reversed);// 确保至少有2个文件if (fileList.size() < 2) {qWarning() << "There are less than 2 files in the folder.";return QString();}// 倒数第二个文件QFileInfo secondNewestFile = fileList.at(fileList.size() - 2);return secondNewestFile.absoluteFilePath();
}

4、拷贝文件夹

//拷贝文件:
void copyFile(const QString &sourcePath, const QString &destinationDir)
{QFile sourceFile(sourcePath);QString destinationPath = QDir(destinationDir).absoluteFilePath(QFileInfo(sourceFile).fileName());if (sourceFile.exists()) {if (!QFile::copy(sourcePath, destinationPath)) {qDebug() << "文件复制失败: " << sourcePath << " 到 " << destinationPath;}else {qDebug() << "文件复制成功: " << sourcePath << " 到 " << destinationPath;}}else {qDebug() << "源文件不存在: " << sourcePath;}
}

注:如果本文章对您有所帮助,请点赞收藏支持一下,谢谢。^_^

版权声明:本文为博主原创文章,转载请附上博文链接。

http://www.xdnf.cn/news/9556.html

相关文章:

  • uni-app开发特殊社交APP
  • 战略3.4 - 研究与开发战略、生产运营战略
  • C++ string 相关的函数
  • 字符串day8
  • 制造业数字化转型之路:再造盈利模式,重塑客户价值
  • 深入理解JavaScript设计模式之call,apply,this
  • 【LangGraph 入门指南】为智能代理打造灵活可控的工作流框架
  • 运维三剑客——sed
  • Redis 的内存回收机制
  • HAWQ 取代传统数仓实践(十八) —— 层次维度
  • DeePNAP:一秒预测蛋白-DNA/蛋白-RNA结合强度
  • 安装nginx
  • Qt使用智能指针
  • 桌面系统核桃派部署自启服务
  • 传感器技术的演进与测试方法探究
  • 数据科学 vs. 大数据:一场“烧脑”但有温度的较量
  • Spring AI 多模型智能协作工作流实现指南
  • AI Agent开发第76课-Dify N8n一类的AI流程“出轨“时会爆发什么样的工程灾难
  • 用python制作一个打地鼠游戏
  • 主要国产数据库及其典型应用场景
  • 每天掌握一个Linux命令 - ps
  • 多因素身份鉴别组合方案及应用场景
  • MySQL----视图的创造和使用
  • 篇章六 数据结构——链表(二)
  • 某标杆房企BI平台2.0升级实践
  • 系统思考:心智模式与业务创新
  • LiveGBS海康、大华、宇视、华为摄像头GB28181国标语音对讲及语音喊话:摄像头设备与服务HTTPS准备
  • 工业总线的“F1赛车“与“越野车“:从控制周期解读EtherCAT与CANopen
  • 镍钯金PCB为什么很难做?
  • 伽罗华域(galois field)的乘法计算(异或法)