C++ under windows traverses and transfers all pictures under the folder and sub-files, and packages the program into an exe executable file


foreword

Recently, in order to facilitate the collection of data, it is necessary to group the pictures in each folder and its subfolders of the factory into one folder for uploading, so the function is realized with C++.


1. Function realization

1. Traverse the pictures under the file and its subfolders, and put its path in the container

Here is the operation on windows, the code is as follows:

//遍历文件夹
void finddir(string dirpath,vector<string>&files){
    
    
	//文件句柄 
	//intptr_t  hFile = 0;
	long long  hFile = 0;
	//文件信息 
	struct _finddata_t fileinfo; //文件信息读取结构
	string p; //string类很有意思的一个赋值函数:assign(),有很多重载版本
	if ((hFile = _findfirst(p.assign(dirpath).append("\\*").c_str(), &fileinfo)) != -1)
	{
    
    
		do
		{
    
    
			if ((fileinfo.attrib & _A_SUBDIR)) //判断是否为文件夹
			{
    
    
				if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
				{
    
    
					finddir(p.assign(dirpath).append("\\").append(fileinfo.name), files);//递归当前文件夹
				}
			}
			else  //文件处理
			{
    
    
				//cout << fileinfo.name << endl;
				string filename = fileinfo.name;
				string suffix = filename.substr(filename.find_last_of(".") + 1);
				//cout << suffix << endl;
				if (suffix == "jpg") {
    
    
					files.push_back(p.assign(dirpath).append("\\").append(fileinfo.name));//文件名

				}
			}
		} while (_findnext(hFile, &fileinfo) == 0); //寻找下一个,成功返回0,否则-1
		_findclose(hFile);
	}

}

2. Create a folder

The function here is to judge whether the stored file path exists, and there is no automatic creation. The code is as follows:

//创建文件夹
void createDir(string path) {
    
    
	if (_access(path.c_str(), 0) == -1) {
    
    
		_mkdir(path.c_str());
	} //判断文件夹是否存在,不存在则自动创建
}

3. Transfer files

Move the picture to the specified directory, move

//移动文件
bool move_file(string imgpath1, string imgpath2) {
    
    
	//imgpath1:原先路径
	//imgpath2:要转移的路径
	BOOL flag = MoveFile(imgpath1.c_str(), imgpath2.c_str()); //若imgpath2不存在会返回0
	return flag;

4. Call code

int main()
{
    
    
	string dirpath = "";//待转移目录
	if (_access(dirpath.c_str(), 0) == -1) {
    
    
		cout << "该目录不存在" << endl;
	}
	vector<string> file_list;
	finddir(dirpath,file_list);
	//cout << "finished" << endl;

	string save_path = "";//目标目录
	createDir(save_path);

	//转移文件
	clock_t s,e;
	s = clock();
	for (int i = 0; i < file_list.size(); i++) {
    
    
		string imgname = file_list[i].substr(file_list[i].find_last_of("\\")+1);
		//cout << file_list[i] << ",\t"<<save_path + "\\" + imgname << endl;
		move_file(file_list[i], save_path + "\\" + imgname);
	}
	e = clock();
	double total_time = double(e - s) / CLOCKS_PER_SEC;
	cout << "Total time:" << total_time <<"s" << endl;
	
}

5. Required header files

#include <iostream>
#include <vector>
#include <string>
#include<io.h>  //必不可少
#include<direct.h>
#include<fstream>
#include <Windows.h>
#include <ctime>
#include <cstdlib>
using namespace std;

Two, package exe

1. Extension installation

The tool we need to use here is Microsoft Visual Studio Installer Project. The installation method is as follows. I personally recommend the second method:
(1): Select Tools in VS—>Extension and Update—>Online, and then search directly, as shown in the figure:
insert image description here

insert image description here
If it is installed, the part displayed in the green box will be the same as mine. Personally, I don’t recommend this method, because it’s too slow to be true. There are suggestions to replace it with a hotspot installation. I haven’t tried this, and interested friends can try it.
(2), the downloaded extension (Xunlei is recommended), double-click to run the installation, the official website link is as follows: Microsoft Visual Studio Installer Project
Note that the version should be consistent with the VS version you use

2. Generate executable files

Here I mainly refer to the article of this big guy. The article is very detailed, with both pictures and texts. There are no problems along the way. The link is as follows: Packing program


Summarize

The above is the whole content of this article. I am also at the entry level of C++. If you have any questions, please point out

Guess you like

Origin blog.csdn.net/qq_55068938/article/details/127733162