Linux程序开发(十):文件分类器趣味设计

Tips:"分享是快乐的源泉💧,在我的博客里,不仅有知识的海洋🌊,还有满满的正能量加持💪,快来和我一起分享这份快乐吧😊!

喜欢我的博客的话,记得点个红心❤️和小关小注哦!您的支持是我创作的动力!数据源存放在我的资源下载区啦!

Linux程序开发(十):文件分类器趣味设计

题目:文件分类器

题目描述:

小明是一个喜欢整理文件的人,他希望编写一个程序来帮助他自动将指定文件夹中的文件按照类型进行分类。具体来说,他想将图片文件(以.jpg或.png结尾)、音频文件(以.mp3或.wav结尾)和文本文件(以.txt结尾)分别移动到对应的类别文件夹中。
请你编写一个C程序,实现小明的需求。程序需要满足以下要求:
1、将源文件夹中的文件按照类型分类,并移动到对应的目标文件夹中。
2、源文件夹和目标文件夹的路径由程序输入时提供,可以相对路径或绝对路径。
3、程序需要递归地处理子文件夹中的文件。
4、如果目标文件夹不存在,则需要创建它。
5、移动文件时,需要保留原文件的权限和时间戳。

示例输入:

源文件夹路径:/root/homework/qm_1/Documents
目标文件夹路径:/root/homework/qm_1/SortedFiles

示例输出:

文件分类完成!

提示:

可以使用Linux系统函数opendir()、readdir()、closedir()来遍历源文件夹中的文件和子文件夹。
可以使用Linux系统函数stat()获取文件的信息,包括文件类型和权限等。
可以使用Linux系统函数mkdir()创建目标文件夹。
可以使用Linux系统函数rename()移动文件。

解答

# docu_sort.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#define MAX_PATH_SIZE 1024
// 判断路径是否是目录
int is_directory(const char *path) {
    struct stat path_stat;
    stat(path, &path_stat);
    return S_ISDIR(path_stat.st_mode);
}
// 创建目录
int create_directory(const char *path) {
    if (mkdir(path, 0777) == -1) {
        return -1;
    }
    return 0;
}
// 移动文件
void move_file(const char *src_path, const char *dst_path) {
    if (rename(src_path, dst_path) == -1) {
        printf("Failed to move %s\n", src_path);
    }
}
// 对文件进行分类
void sort_files(const char *src_dir, const char *dst_dir) {
    DIR *dir;
    struct dirent *ent;
    char file_path[MAX_PATH_SIZE];
    char dst_path[MAX_PATH_SIZE];

    // 打开源文件夹
    if ((dir = opendir(src_dir)) != NULL) {
        // 遍历源文件夹中的每个文件
        while ((ent = readdir(dir)) != NULL) {
            // 忽略 "." 和 ".."
            if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) {
                continue;
            }
            // 构造文件的完整路径
            snprintf(file_path, sizeof(file_path), "%s/%s", src_dir, ent->d_name);
            // 判断文件是否是目录
            if (is_directory(file_path)) {
                // 如果是目录,则递归处理子目录
                char new_src_dir[MAX_PATH_SIZE];
                char new_dst_dir[MAX_PATH_SIZE];
                strcpy(new_src_dir, src_dir);
                strcat(new_src_dir, "/");
                strcat(new_src_dir, ent->d_name);
                strcpy(new_dst_dir, dst_dir);
                strcat(new_dst_dir, "/");
                strcat(new_dst_dir, ent->d_name);
                create_directory(new_dst_dir);
                sort_files(new_src_dir, new_dst_dir);
            } else {
                // 根据文件扩展名确定目标路径
                char *extension = strrchr(ent->d_name, '.');
                if (extension == NULL) {
                    strcpy(dst_path, dst_dir);
                    strcat(dst_path, "/unknown/");
                } else if (strcmp(extension, ".txt") == 0) {
                    strcpy(dst_path, dst_dir);
                    strcat(dst_path, "/text/");
                } else if (strcmp(extension, ".mp3") == 0 || strcmp(extension, ".wav") == 0) {
                    strcpy(dst_path, dst_dir);
                    strcat(dst_path, "/audio/");
                } else if (strcmp(extension, ".jpg") == 0 || strcmp(extension, ".png") == 0) {
                    strcpy(dst_path, dst_dir);
                    strcat(dst_path, "/image/");
                } else {
                    strcpy(dst_path, dst_dir);
                    strcat(dst_path, "/unknown/");
                }
                // 创建目标目录(如果不存在)
                create_directory(dst_path);
                // 构造目标文件的完整路径
                strcat(dst_path, ent->d_name);

                // 移动文件到目标路径
                move_file(file_path, dst_path);
            }
        }
        closedir(dir);  // 关闭源文件夹
    } else {
        printf("Failed to open directory %s\n", src_dir);
    }
}
int main() {
    char src_dir[MAX_PATH_SIZE];
    char dst_dir[MAX_PATH_SIZE];
    printf("请输入源文件夹路径:");
    fgets(src_dir, sizeof(src_dir), stdin);
    src_dir[strcspn(src_dir, "\n")] = '\0';  // 去掉末尾的换行符

    printf("请输入目标文件夹路径:");
    fgets(dst_dir, sizeof(dst_dir), stdin);
    dst_dir[strcspn(dst_dir, "\n")] = '\0';  // 去掉末尾的换行符

    // 自动创建目标文件夹
    create_directory(dst_dir);

    // 对文件进行分类
    sort_files(src_dir, dst_dir);

    printf("文件分类完成!\n");

    return 0;
}

截图:

在这里插入图片描述
图 1.1 源文件目录图
在这里插入图片描述
图 1.2 编译运行程序图
在这里插入图片描述
图 1.3 目标文件目录图

  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卡林神不是猫

如果您觉得有帮助可以鼓励小卡哦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值