c语言递归函数实现目录复制,C语言(递归遍历文件夹)实现文件批量复制

#include/*

1.文件批处理的快速操作,

2.当前电脑中不存在的功能

3.实用性较高

4.速度较快

5.需求功能

5.1 大文件夹下搜索指定文件名文件 复制到指定对应文件路径下 部分文件指定复制 是否覆盖大

5.2 文件夹下搜索指定文件名文件 复制到某个文件夹下 模糊抽出功能

*/

/*

字符串匹配算法

*/

int Inquire_filaname(char *src, char *dest)

{

int i = 0, j = 0;

int srclen = 0, destlen = 0;

srclen = strlen(src);

destlen = strlen(dest);

if (srclen < destlen)

{

return -1;

}

while (i < srclen && j < destlen)

{

if (src[i] == dest[j])

{

i++;

j++;

}

else

{

i = i - j + 1;

j = 0;

}

}

if (j >= destlen)

{

return i - j;

}

else

{

return -1;

}

}

/*

将Inpath路径的文件,复制到Outpath的路径中

*/

int WriteFile(char *Inpath, char *Outpath)

{

FILE *Infile=NULL ;

FILE *Outfile =NULL;

char str[1024] ;

if (Inpath != NULL && Outpath != NULL)

{

Infile = fopen(Inpath, "rb");

Outfile = fopen(Outpath, "wb");

if (Infile != NULL)

{

if (Outfile != NULL)

{

//printf("%c", getc(Infile));

while (fread(str, sizeof(str),1 , Infile) != NULL)

{

fwrite(str, sizeof(str),1, Outfile);

}

}

fclose(Outfile);

fclose(Infile);

Infile = NULL;

Outfile = NULL;

return 2;

}

}

return 0;

}

/*

指定搜索目录

指定目的目录

指定搜索文件名

指定搜索类型

*/

int File_out(char *SrcPath, char *filename,char *DestPath, char *type)

{

int a = atoi(type);

if (SrcPath != NULL && DestPath != NULL) // 如果指定大 文件目录存在

{

_mkdir(DestPath);

// type ==1 5.1 type == 2 5.2

FileSearch(SrcPath, filename, DestPath, a);

}

else

{

printf("请输入输入路径或输入路径");

return 0;

}

}

int FileSearch(char *dir, char *filename, char *copypath, int type)//递归遍历当前目录下的所有文件

{

char dirNew[200];

int Isnull = 0; //文件夹中是否有符合的文件,0为没有,1为有

char path_A[200]; //保存文件路径

char path_B[200]; //保存文件夹路径

strcpy(dirNew, dir);

strcat(dirNew, "\\*.*"); // 修改此处改变搜索条件

struct _finddata_t filefind;

int done = 0, i, handle;

if ((handle = _findfirst(dirNew, &filefind)) != -1)

{

while (!(done = _findnext(handle, &filefind)))

{

strcpy(path_A, dir);

strcpy(path_B, copypath);

if (strcmp(filefind.name, "..") == 0)

continue;

printf("当前工作目录 is \"%s\"\n", dir);

printf("当前目的目录 is \"%s\"\n", copypath);

if ((_A_SUBDIR == filefind.attrib))// 是目录

{

//printf("当前工作目录 is \"%s\"\n", dirNew);

//截取最后路径/

if (type == 1)

{

strcat(path_B, "\\");

strcat(path_B, filefind.name);

_mkdir(path_B);

}

strcat(path_A, "\\");

strcat(path_A, filefind.name);

if (1 == FileSearch(path_A, filename, path_B, type))// 递归遍历子目录

{

memset(path_A, '\0', sizeof(path_A));

memset(path_B, '\0', sizeof(path_B));

strcpy(path_A, dir);

strcpy(path_B, copypath);

}

}

else

{

if (-1 != Inquire_filaname(filefind.name, filename))

{

//printf("[File]:\t%s\n", filefind.name);

//如果是5.1,则在之前创建的文件夹下,复制文件

if (type == 1)

{

strcat(path_B, "\\");

strcat(path_B, filefind.name);

//copypath = path_B;

//printf("[copypath]:\t%s\n", copypath);

}

else

{

//如果是5.2,则直接在总路径下复制路径文件,

strcat(path_B, "\\");

strcat(path_B, filefind.name);

//copypath = path_B;

}

strcat(path_A, "\\");

strcat(path_A, filefind.name);

//(copypath, filefind.name);

printf("[path_A]:\t%s\n", path_A);

printf("[path_B]:\t%s\n", path_B);

if (WriteFile(path_A, path_B) != 0)

{

Isnull = 1;

}

}

}

}

_findclose(handle);

//如果出现空文件夹或不是2类型,则删除空文件夹

if (Isnull == 0 && 1 == type)

{

_rmdir(copypath);

}

return 1;

}

return 0;

}

int main(int argc, char **argv)

{

File_out(dirname, dirname1, filename, argv[4]);

return 0;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值