/*
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
新学的C语言,自己写了个文件夹自动同步的程序。
最新推荐文章于 2022-09-03 17:59:28 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)