写一个删除C:\test目录十分之前的文件的代码
时间: 2024-02-11 21:07:21 浏览: 34
以下是 Python 代码实现:
```python
import os
import time
dir_path = "C:/test"
expire_time = time.time() - 864000 # 864000 秒为十天
for root, dirs, files in os.walk(dir_path, topdown=False):
for name in files:
file_path = os.path.join(root, name)
if os.stat(file_path).st_mtime < expire_time:
os.remove(file_path)
print(f"{file_path} has been deleted.")
```
以上代码会遍历 `C:\test` 目录及其子目录下的所有文件,如果文件的最后修改时间早于十天前,则会将其删除。如果目录下有很多文件,这个过程可能会比较耗时,请耐心等待。另外,建议在运行之前先备份好重要的文件。
相关问题
用C语言或C++写一个删除windows电脑上C:\test目录十分之前的文件的代码
以下是 C++ 代码实现:
```c++
#include <iostream>
#include <filesystem>
#include <chrono>
namespace fs = std::filesystem;
using namespace std::chrono;
int main()
{
fs::path dir_path = "C:/test";
auto expire_time = duration_cast<seconds>(system_clock::now().time_since_epoch()).count() - 864000; // 864000 秒为十天
for (const auto& entry : fs::recursive_directory_iterator(dir_path))
{
if (entry.is_regular_file() && entry.last_write_time().time_since_epoch().count() < expire_time)
{
fs::remove(entry.path());
std::cout << entry.path() << " has been deleted." << std::endl;
}
}
return 0;
}
```
以上代码使用了 C++17 中的文件系统库,会遍历 `C:\test` 目录及其子目录下的所有文件,如果文件的最后修改时间早于十天前,则会将其删除。如果目录下有很多文件,这个过程可能会比较耗时,请耐心等待。另外,建议在运行之前先备份好重要的文件。
使用C语言写了一个删除C:\test目录十分钟之前的jpg图片的代码
以下是使用C语言实现删除C:\test目录十分钟之前的jpg图片的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#define DIR_PATH "C:/test" // 需要删除文件的目录路径
#define DEL_TIME 600 // 删除时间(秒)
bool is_jpg(char *filename) {
char *ext = strrchr(filename, '.');
if (ext != NULL && strcmp(ext, ".jpg") == 0) {
return true;
}
return false;
}
bool is_within_time(time_t mtime, int seconds) {
time_t now = time(NULL);
time_t diff = now - mtime;
if (diff > seconds) {
return false;
}
return true;
}
void delete_file(char *filepath) {
remove(filepath);
printf("Deleted %s\n", filepath);
}
void delete_old_jpg(char *dirpath, int seconds) {
DIR *dir = opendir(dirpath);
if (dir == NULL) {
fprintf(stderr, "Error opening directory %s\n", dirpath);
return;
}
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
char filepath[512];
sprintf(filepath, "%s/%s", dirpath, entry->d_name);
struct stat sbuf;
if (stat(filepath, &sbuf) != -1) {
if (S_ISREG(sbuf.st_mode) && is_jpg(entry->d_name) && is_within_time(sbuf.st_mtime, seconds)) {
delete_file(filepath);
}
}
}
closedir(dir);
}
int main() {
delete_old_jpg(DIR_PATH, DEL_TIME);
return 0;
}
```
该代码通过 opendir、readdir、stat 和 remove 函数实现了遍历目录、获取文件信息以及删除文件的功能。主要步骤如下:
1. 定义了两个辅助函数 is_jpg 和 is_within_time,用于判断文件是否为 jpg 格式和是否在指定时间之内。
2. 定义了 delete_file 函数,用于删除指定文件。
3. 定义了 delete_old_jpg 函数,用于遍历目录,获取文件信息,判断是否需要删除,并调用 delete_file 函数删除文件。
4. 在 main 函数中调用 delete_old_jpg 函数,删除指定目录中十分钟之前的 jpg 图片。
值得注意的是,该代码仅供参考,实际使用中需要根据具体情况进行修改和测试。特别是删除文件操作有一定风险,需要谨慎处理。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)