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

c++ boost : 保留最新文件其余删除

#include <boost/filesystem.hpp>
#include <iostream>
#include <stdio.h>

/*
 * delete_file_and_dir - 删除指定路径下文件和目录,保留指定个数的新文件
 *  @path : 路径
 *  @reserved_quantity : 保留新文件个数
 */
int delete_file_and_dir(const std::string &path, int reserved_quantity) {
    std::string cmd;
    std::vector<boost::filesystem::path> __file;
    int res;
    int count = 0;

    if (!boost::filesystem::is_directory(path)) {
        printf("path %s error\n", path.c_str());
        return -1;
    }
    for (auto file : boost::filesystem::directory_iterator(path)) {
        __file.push_back(file.path());
    }

    printf("folder %s have files [%ld], delete to [%d]\n",
                    path.c_str(), __file.size(), reserved_quantity);

    count = __file.size();
    while (count-- > 0) {  /* avoid a dead loop */
        __file.clear();
        for (auto file : boost::filesystem::directory_iterator(path)) {
            __file.push_back(file.path());
        }

        if (static_cast<int>(__file.size()) <= reserved_quantity) {
            break;
        }

        /* 按照修改时间排序,新文件在前 */
        sort(__file.begin(), __file.end(),
                    [](const boost::filesystem::path &a, const boost::filesystem::path &b)->bool {
                        std::time_t t1 = boost::filesystem::last_write_time(a);
                        std::time_t t2 = boost::filesystem::last_write_time(b);
                        return t1 > t2;
                    });

        auto it = --__file.end();

        /* delete directory */
        if (boost::filesystem::is_directory(*it)) {  
            cmd = "rm -rf " + it->string();
            printf("cmd:%s\n", cmd.c_str());
            system(cmd.c_str());
        }
        /* delete file */
        else {
            if (res = remove(it->string().c_str()), 0 == res) {
                printf("delet file {%s}\n", it->string().c_str());
                __file.erase(it);
            } else {
                printf("delete file {%s} failed\n", it->string().c_str());
                return -1;
            }
        }
    }

    return 0;
}

int main(void)
{
    std::string path = "/home/ac-test";

    /* 保留最新3个文件 */
    delete_file_and_dir(path, 3);

    return 0;
}

#if 0
g++ -Wall -o boost-clear-dir boost-clear-dir.o -lboost_filesystem
#endif

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • C语言蓝桥杯:语言基础
  • 小程序的右侧抽屉开关动画手写效果
  • javascript如何打印九九乘法表
  • Computer Exercise
  • 笔记整理—内核!启动!—kernel部分(2)从汇编阶段到start_kernel与内核进程
  • C语言知识体系思维导图
  • 【828华为云征文|手把手教你如何用华为云Flexus X实例部署之前爆火的“人生重启“游戏】
  • EP8 我的页面布局
  • Win10系统Legacy转UEFI
  • 红海云 × 紫光同芯 | 数字化驱动芯片领军企业人力资源管理新升级
  • Vue项目异常:has no default export.Vetur(1192)
  • 基于Flink的流式计算可视化开发实践之配置->任务生成->任务部署过程
  • 动手学深度学习(pytorch土堆)-04torchvision中数据集的使用
  • C++:类与对象——详解继承、多继承、虚继承
  • SQL进阶技巧:每年在校人数统计 | 区间重叠问题
  • 【Leetcode】101. 对称二叉树
  • angular组件开发
  • ECMAScript 6 学习之路 ( 四 ) String 字符串扩展
  • javascript面向对象之创建对象
  • Linux后台研发超实用命令总结
  • miaov-React 最佳入门
  • Octave 入门
  • scrapy学习之路4(itemloder的使用)
  • vue 配置sass、scss全局变量
  • 代理模式
  • 第三十一到第三十三天:我是精明的小卖家(一)
  • 机器学习 vs. 深度学习
  • 模仿 Go Sort 排序接口实现的自定义排序
  • 判断客户端类型,Android,iOS,PC
  • 树莓派 - 使用须知
  • 用简单代码看卷积组块发展
  • 怎么把视频里的音乐提取出来
  • ​LeetCode解法汇总2304. 网格中的最小路径代价
  • "无招胜有招"nbsp;史上最全的互…
  • #pragma 指令
  • (152)时序收敛--->(02)时序收敛二
  • (2)STM32单片机上位机
  • (2015)JS ES6 必知的十个 特性
  • (delphi11最新学习资料) Object Pascal 学习笔记---第8章第2节(共同的基类)
  • (Qt) 默认QtWidget应用包含什么?
  • (SpringBoot)第二章:Spring创建和使用
  • (void) (_x == _y)的作用
  • (vue)el-cascader级联选择器按勾选的顺序传值,摆脱层级约束
  • (阿里云万网)-域名注册购买实名流程
  • (搬运以学习)flask 上下文的实现
  • (层次遍历)104. 二叉树的最大深度
  • (二)WCF的Binding模型
  • (二)延时任务篇——通过redis的key监听,实现延迟任务实战
  • (二刷)代码随想录第16天|104.二叉树的最大深度 559.n叉树的最大深度● 111.二叉树的最小深度● 222.完全二叉树的节点个数
  • (九)One-Wire总线-DS18B20
  • (十)【Jmeter】线程(Threads(Users))之jp@gc - Stepping Thread Group (deprecated)
  • ******IT公司面试题汇总+优秀技术博客汇总
  • .NET 4.0中使用内存映射文件实现进程通讯
  • .net core + vue 搭建前后端分离的框架
  • .NET Core 发展历程和版本迭代