c++创建每日文件夹,放入每日日志

编译使用g++ demo.cpp -std=c++17 确保使用 C++17 及以上版本进行编译 推荐 9.0 以上版本

首先创建logs总文件夹,例如/home/xs/logs

然后创建当前日期的文件夹,例如/home/xs/logs/2024.12.10

随后把文件放入此日期文件夹中,例如/home/xs/logs/2024.12.10/3d.pcd

最后遍历logs下的所有文件夹,判断文件夹日期和当前系统日期差距是多少天,超过设置时间的文件夹会以递归的形式将内容和文件夹全部删除

cpp 复制代码
#include <iostream>
#include <filesystem>
#include <chrono>
#include <fstream>

namespace fs = std::filesystem;

// 定义 days 类型以兼容 C++17
using days = std::chrono::duration<int, std::ratio<86400>>;

// 判断文件夹是否超过一个月
bool isOlderThanOneMonth(const fs::directory_entry &dirEntry) {
    // 获取文件的最后写入时间(文件时钟)
    auto fileTime = dirEntry.last_write_time();

    // 转换为 system_clock 时间点
    auto fileTimeAsSystemClock = std::chrono::time_point_cast<std::chrono::system_clock::duration>(
            fileTime - fs::file_time_type::clock::now() + std::chrono::system_clock::now()
    );

    // 获取当前时间
    auto now = std::chrono::system_clock::now();

    // 计算时间差并判断是否超过 30 天
    auto age = std::chrono::duration_cast<days>(now - fileTimeAsSystemClock).count();
    return age > 30;
}

// 创建以今日日期命名的文件夹
std::string createTodayFolder(const std::string &baseDir) {
    auto t = std::time(nullptr);
    auto tm = *std::localtime(&t);
    char folderName[100];
    std::strftime(folderName, sizeof(folderName), "%Y-%m-%d", &tm);
    std::string todayFolder = baseDir + "/" + folderName;

    if (!fs::exists(todayFolder)) {
        fs::create_directory(todayFolder);
        std::cout << "Created folder: " << todayFolder << std::endl;
    } else {
        std::cout << "Folder already exists: " << todayFolder << std::endl;
    }
    return todayFolder;
}

// 删除超过一个月的文件夹
void cleanupOldFolders(const std::string &baseDir) {
    for (const auto &entry: fs::directory_iterator(baseDir)) {
        if (fs::is_directory(entry) && isOlderThanOneMonth(entry)) {
            fs::remove_all(entry);
            std::cout << "Deleted old folder: " << entry.path() << std::endl;
        }
    }
}

int main() {
    std::string baseDir = "logs";//文件目录
    // 创建主目录
    if (!fs::exists(baseDir)) {
        fs::create_directory(baseDir);
    }
    // 创建今日文件夹
    std::string todayFolder = createTodayFolder(baseDir);
    // 复制文件到今日文件夹
    std::string sourceFilePath = "/home/xs/BOX_1.pcd";  //要移动到文件夹里面的而文件
    std::string destinationFilePath = todayFolder + "/2d.pcd"; //移动到logs/todayFolder里面,命名为2d.pcd

    try {
        fs::copy(sourceFilePath, destinationFilePath, fs::copy_options::overwrite_existing);
        std::cout << "Copied file to: " << destinationFilePath << std::endl;
    } catch (const fs::filesystem_error &e) {
        std::cerr << "Error copying file: " << e.what() << std::endl;
    }

    // 清理一个月前的文件夹
    cleanupOldFolders(baseDir);

    return 0;
}
相关推荐
我是唐青枫几秒前
C#.NET Monitor 与 Mutex 深入解析:进程内同步、跨进程互斥与使用边界
开发语言·c#·.net
bbq粉刷匠几秒前
Java--剖析synchronized
java·开发语言
ou.cs4 分钟前
c# 信号量和锁的区别
开发语言·c#
Gofarlic_OMS4 分钟前
装备制造企业Fluent许可证成本分点典型案例
java·大数据·开发语言·人工智能·自动化·制造
Freak嵌入式15 分钟前
MicroPython LVGL基础知识和概念:显示与多屏管理
开发语言·python·github·php·gui·lvgl·micropython
SccTsAxR19 分钟前
算法基石:手撕离散化、递归与分治
c++·经验分享·笔记·算法
yu859395819 分钟前
matlab雷达信号与干扰的仿真
开发语言·matlab
前进的李工20 分钟前
LangChain使用AI工具赋能:解锁大语言模型无限潜力
开发语言·人工智能·语言模型·langchain·大模型
yugi98783826 分钟前
C# 串口下载烧写BIN文件工具
开发语言·c#
Q741_14744 分钟前
每日一题 力扣 3655. 区间乘法查询后的异或 II 模拟 分治 乘法差分法 快速幂 C++ 题解
c++·算法·leetcode·模拟·快速幂·分治·差分法