C语言实现批量复制文件

假设你拿到一个数据集,里面包含很多的文件夹,每个文件夹下都有你想要复制的文件。(包括文本文件和二进制文件)

程序思路分为三步:

1.制作一份.txt文件,每一行存放要复制文件的路径

2.制作一份.txt文件,每一行存放复制后文件保存的路径

3.利用文件复制函数进行复制

利用bat批处理命令 DIR *.* /B > 文件名列表.txt,提取文件夹下所有文件夹名称。

利用fprintf函数制作要复制文件的路径.txt文件(文件名列表2.txt),同理制作保存文件的路径(文件名列表3.txt)

#include <stdio.h>
#include <iostream>
using namespace std;
struct add
{
	char a[10];
	char b[10];
}; 
typedef struct add ADD;

int main()
{
	ADD *addbuff = new ADD[436];
	int total = 0; 
	FILE *fpRead = fopen("G://s3c//Data//文件名列表.txt", "r");
	FILE *fpWrite = fopen("G://s3c//Data//文件名列表2.txt", "w");

	if (fpRead != NULL)
	{
		//while (!feof(fpRead))
		for (total = 0; total < 436; total++)
		{
			fscanf(fpRead, "%s ", addbuff[total].a);
			fprintf(fpWrite, "G://s3c//Data//%s//%s.osgb\n", addbuff[total].a, addbuff[total].a);
			cout << addbuff[total].a << endl;
		}
		fclose(fpRead);
		fclose(fpWrite);
	}

	return 0;

} 

复制文件

#include <stdio.h>
#include <iostream>
using namespace std;

struct path
{
	char a[10];
};
typedef struct path PATH;

int CopyFile(char* SrcFile, char* DesFile)
{
	FILE* fp, *fw;
	long length;
	long n, cpyfinish;
	char buf[1024];
	if ((fp = fopen(SrcFile, "rb")) == NULL)
	{
		return 1;
	}
	fw = fopen(DesFile, "wb");

	fseek(fp, 0L, SEEK_END);
	length = ftell(fp);
	rewind(fp);
	while (length>0)
	{
		n = fread(buf, 1, 1024, fp);
		cpyfinish = fwrite(buf, 1, n, fw);
		length -= cpyfinish;
	}
	fclose(fw);
	fclose(fp);
	return 0;
}


int main()
{
	PATH *srcpath = new PATH[436];
	PATH *dstpath = new PATH[436];
	int total = 0;
	FILE *fpRead_1 = fopen("G://s3c//Data//文件名列表2.txt", "r");
	FILE *fpRead_2 = fopen("G://s3c//Data//文件名列表3.txt", "r");

		
		for (total = 0; total < 436; total++)
		{
			fscanf(fpRead_1, "%s ", srcpath[total].a);
			fscanf(fpRead_2, "%s ", dstpath[total].a);
			
			cout << srcpath[total].a << endl;
			cout << dstpath[total].a << endl;
			CopyFile(srcpath[total].a, dstpath[total].a);
		}

		return 0;

}

可以参考https://blog.csdn.net/qq_32579021/article/details/81738766

猜你喜欢

转载自blog.csdn.net/Xiao_Xue_Seng/article/details/95345216