c++代码实现logs文件夹下每隔十分钟如果当前时间分钟数是10的倍数,则生成一个文件夹名为当前的时间,格式为: %Y-%m-%d-%H-%M的时间文件夹,文件夹命名时间分钟必须为十的倍数,当logs文件夹的总容量超过10G时,会根据文件夹的名字转化为时间后自动删除最早的文件夹
时间: 2024-03-17 21:40:31 浏览: 28
好的,以下是c++代码实现logs文件夹下每隔十分钟如果当前时间分钟数是10的倍数,则生成一个文件夹名为当前的时间,格式为:%Y-%m-%d-%H-%M的时间文件夹,文件夹命名时间分钟必须为十的倍数,当logs文件夹的总容量超过10G时,会根据文件夹的名字转化为时间后自动删除最早的文件夹的程序:
```c++
#include <iostream>
#include <ctime>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
using namespace std;
// 获取当前时间
string get_current_time() {
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
strftime(buf, sizeof(buf), "%Y-%m-%d-%H-%M", &tstruct);
return buf;
}
// 判断当前时间分钟数是否为10的倍数
bool is_ten_minute() {
time_t now = time(0);
struct tm tstruct;
tstruct = *localtime(&now);
return tstruct.tm_min % 10 == 0;
}
// 获取文件夹大小
unsigned long long get_folder_size(string folder_path) {
unsigned long long size = 0;
DIR *dir;
struct dirent *ent;
struct stat statbuf;
if ((dir = opendir(folder_path.c_str())) != NULL) {
while ((ent = readdir(dir)) != NULL) {
string path = folder_path + "/" + ent->d_name;
lstat(path.c_str(), &statbuf);
if (!S_ISDIR(statbuf.st_mode)) {
size += statbuf.st_size;
}
}
closedir(dir);
}
return size;
}
// 删除最早的文件夹
bool delete_earliest_folder(string folder_path) {
DIR *dir;
struct dirent *ent;
vector<string> folder_names;
if ((dir = opendir(folder_path.c_str())) != NULL) {
while ((ent = readdir(dir)) != NULL) {
if (ent->d_type == DT_DIR && string(ent->d_name) != "." && string(ent->d_name) != "..") {
folder_names.push_back(ent->d_name);
}
}
closedir(dir);
}
sort(folder_names.begin(), folder_names.end());
if (!folder_names.empty()) {
string earliest_folder_name = folder_names[0];
string earliest_folder_path = folder_path + "/" + earliest_folder_name;
if (remove(earliest_folder_path.c_str()) != 0) {
return false;
}
}
return true;
}
int main() {
string folder_path = "./logs";
while (true) {
if (is_ten_minute()) {
string current_time = get_current_time();
string folder_name = folder_path + "/" + current_time;
mkdir(folder_name.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
}
unsigned long long folder_size = get_folder_size(folder_path);
if (folder_size > 10 * 1024 * 1024 * 1024) {
delete_earliest_folder(folder_path);
}
sleep(600); // 每隔十分钟执行一次
}
return 0;
}
```
代码说明:
1. 在 `main()` 函数中,使用一个死循环,每隔十分钟执行一次。
2. 在每次循环中,判断当前时间分钟数是否为10的倍数,如果是,则创建一个以当前时间命名的文件夹;然后获取 logs 文件夹的总容量,如果超过10G,则删除最早的文件夹。
3. 使用 `sleep(600)` 函数使程序每隔十分钟执行一次。
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)