C language realizes batch copying of files

Suppose you get a data set, which contains many folders, and each folder contains the files you want to copy. (Including text files and binary files)

The program idea is divided into three steps:

1. Make a .txt file, each line stores the path of the file to be copied

2. Make a .txt file, and store the path where the copied file is saved in each line

3. Use the file copy function to copy

Use the bat batch command DIR *.* /B> ​​file name list.txt to extract all the folder names under the folder.

Use the fprintf function to make the path of the file to be copied. Txt file (file name list 2.txt), and similarly make the path to save the file (file name list 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;

} 

Copy files

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

}

You can refer to https://blog.csdn.net/qq_32579021/article/details/81738766

Guess you like

Origin blog.csdn.net/Xiao_Xue_Seng/article/details/95345216