新学的C语言,自己写了个文件夹自动同步的程序。

这篇博客分享了作者初次学习C语言后,动手编写的一个文件夹自动同步程序的实践过程,详细介绍了如何利用C语言进行文件操作和实现同步功能。
摘要由CSDN通过智能技术生成
/*
Created By Richard Shao 2021.09.09
Synchronize source folder to dest folder.
Only for windows.
argv
 [1] : source directory
 [2] : dest directory
 [3] : log directory     -> log file name: time stamp
*/
#include <stdio.h>
#include "stdlib.h" 
#include <sys/stat.h> 
#include <string.h>        // for strcpy(), strcat()
#include <sys/types.h>
#include <math.h>
#include <time.h>
#include <windows.h> //win头文件

#define  MAX_FILES 100000//文件数量
#define  PATH_LEN  1000

char src[PATH_LEN];
char dst[PATH_LEN];
char log_dir[PATH_LEN];
char log_file[PATH_LEN];
char log_path[PATH_LEN];

char base_dir[PATH_LEN];
char mkdir_cmd[PATH_LEN];

FILE *fp; 

int del_log  = 1;
int base_len = 0;
long src_num = 0;
long dst_num = 0;
int empty_folder_flag = 0;

size_t src_len = 0;
size_t dst_len = 0;
size_t log_len = 0;

typedef struct Data{
    char dir[PATH_LEN];
    unsigned long size;
    unsigned long time_write;
}Data;

struct Data src_files[MAX_FILES];
struct Data dst_files[MAX_FILES];

int set_and_chk_arg(int argc,char *argv[]);
int sync(void);
int get_files(const char *dir,const char *s_d,int flg);
char *get_file_path(char *path);
int check_empty_folder(const char *dir);

/******************************************************************************
MAIN Richard Shore 2021.09.09
*******************************************************************************/
int     main(argc,argv)
int     argc ;
char    *argv[] ;
{
    time_t start_time=time(NULL);
    struct tm * start_lt = localtime(&start_time);//convert to time struct
    sprintf(log_file,"mysync_log_%d%02d%02d%02d%02d%02d",
                start_lt->tm_year+1900, 
                start_lt->tm_mon+1, 
                start_lt->tm_mday, 
                start_lt->tm_hour, 
                start_lt->tm_min, 
                start_lt->tm_sec);//month must + 1

    /*Check if arguments are right.*/
    int set_and_chk_arg_result = 0;
    set_and_chk_arg_result = set_and_chk_arg(argc,argv) ;
    if (set_and_chk_arg_result == 1) 
    {
        return 1;
    }

    //log file
    strcpy(log_path,log_dir);
    strcat(log_path,"\\");
    strcat(log_path,log_file);
    printf("src: %s\t dst: %s\t log: %s\n",src,dst,log_path);

    if ((fp = fopen(log_path, "w")) == NULL)
    {
        printf("Failed to open file: %s",log_path);
        return -1;
    }
    fprintf(fp,"mysync start %s\n",log_file);

    //set_and_chk_arg(argc,argv);
    // Folder synchronize
    sync();

    time_t end_time=time(NULL);
    fprintf(fp,"mysync end.\n");
    fprintf(fp,"Time cost: %d\n",end_time - start_time);
    fclose(fp);
    printf("Time cost: %d\n",end_time - start_time);

    //If source=dest del
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值