#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;

}

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

  1. C语言判断文件夹或者文件是否存在的方法

    C语言判断文件夹或者文件是否存在的方法 方法一:access函数判断文件夹或者文件是否存在 函数原型: int access(const char *filename, int mode); 所属头文 ...

  2. python跳转和创建目录,Python - 创建目录,切换目录,创建文件,复制文件,移动文件 ,删除文件夹及文件...

    # -*- coding: utf-8 -*- """ Created on Mon Jan  9 00:35:43 2017 @author-Weixin: 95355 ...

  3. R语言对文件夹及文件的操作函数

    文章目录 连接形成文件路径 读取指定路径中的全部文件名 删除文件 打开文件 重命名文件 复制文件 复制文件到指定文件夹 覆盖复制 追加复制 返回上一级路径 查看某一文件夹是否存在 创建文件夹 删除文件 ...

  4. golang 文件 文件夹 创建 读取 移动 复制 写入 遍历

    目录 文件读取 创建 写入 移动 拷贝 删除 文件 创建文件夹 文件覆盖判断 遍历文件夹 权限 文件操作, 大体分为两部分: os库 : 文件\文件夹创建,读取,移动,复制 io库 : 文件内容的写入 ...

  5. linux下文件夹的创建、复制、剪切、重命名、清空和删除命令

    在home目录下有wwwroot目录,wwwroot下有sinozzz目录,即/home/wwwroot/sinozzz 一.目录创建 在/home/wwwroot目录下新建一个sinozzz123的 ...

  6. linux复制文件夹 实例,linux复制文件夹与文件实例介绍linux操作系统 -电脑资料

    本文章来介绍一下关于linux复制文件夹与文件的一些基础用法,有需要学习的朋友可参考一下本文章, CP命令 格式: CP [选项] 源文件或目录  目的文件或目录 选项说明:-b 同名,备分原来的文件 ...

  7. linux复制压缩文件夹,[操作系统]linux中文件或者文件夹的基本操作(复制,移动,删除,查找,压缩)...

    [操作系统]linux中文件或者文件夹的基本操作(复制,移动,删除,查找,压缩) 0 2015-12-28 00:00:15 linux 文件(文件夹)的创建,复制,移动,重命名,删除基本命令 复制文 ...

  8. 按文件夹名匹配并复制文件夹及子文件

    先说一下功能概要: 将文件或文件夹以关键字开头的名字复制到另一个目录下. 附加功能:定时检测源目录是否有新增文件或文件夹,并实时拷贝. //拷贝文件/夹的方法public class CopyFold ...

  9. Linux下文件夹的移动与复制

    Linux下文件夹的移动与复制 封面图片由文心一格AI生成 在Linux系统中,移动和复制文件夹是常见的任务.无论你是想要备份数据.迁移文件夹到其他位置还是归档数据,掌握文件夹的移动和复制技能都是必要 ...

  10. C++ Qt C#文件夹重命名 C#复制文件(夹)

    一.C++文件夹重命名方法: 使用MoveFile()方法即可,源文件夹名字和目标文件夹名字都需要完整的路径名称 if (g_oCamera[0].strCodedData != _T("& ...

最新文章

  1. 从冬奥会开幕式谈起,AI的艺术造诣如何?
  2. 自学python数据分析要多久-数据分析培训班要学习多长时间?
  3. list中抽出某一个字段的值_Java的stream代替List解决单线程等问题
  4. vim 树形目录插件NERDTree安装及简单用法
  5. 【NOIP2002】【Luogu1037】产生数(高精乘低精,DFS暴力搜索)
  6. YOLO系列:YOLO v2深度解析 v1 vs v2
  7. css3的新属性 新增的颜色--- 透明度---两种渐变---定义多张背景图--background-size...
  8. Python替换多个tomcat端口号
  9. VS编译时自动引用Debug|Release版本的dll
  10. 【背包问题】基于matlab模拟退火算法求解背包问题【含Matlab源码 108期】
  11. python读取txt文件数据并存到list中
  12. Lottie动画概述,文末有彩蛋
  13. Windows下winrm的网络类型公用的问题解决
  14. 局域网中的通信子网和资源子网
  15. centos设置自动开关机
  16. 互联网企业实习面试经验分享(谷歌微软hulu阿里腾讯字节美团百度等等)
  17. 数据约束 for:麻包缝裤衩
  18. oracle怎么使用Xmanager,Xmanager使用总结
  19. 跑跑卡丁车最新等级奖励2016
  20. 在html中怎么设置段落的边框线,Word怎么设置文字段落边框和底纹

热门文章

  1. python bad file descriptor_Errno 9: Bad file descriptor in python socket错误处理
  2. php流量计算单位,数据业务流量的计量单位及换算
  3. 高老师的架构设计_隽语集(BB_0901)
  4. 2020蓝桥杯省赛C/C++B组(第二场) 试题A:门牌制作
  5. Python海象运算符的使用
  6. 用快捷键在浏览器中浏览网页不用鼠标
  7. 采用Spring配置文件管理Bean(2)
  8. 微信卡券H5投放开发
  9. rocksdb原理_RocksDB解析
  10. windows mobile linux,搞定linux下与windows mobile 5手机的同步