如何将某个文件夹下的文件根据标签分到指定的文件夹下

      在平时的工作中,有时候文件夹下面有很多的文件,但是只有部分是我们需要的,如何将这部分文件提取出来并根据我们所拥有的标签分好类,是做分类中一个很重要的环节。

比如上面一共有53576个文件,但是我们只要其中10000多个文件,且这10000多个文件有标签,如何根据标签分好类并放入文件夹呢?下面直接上代码

#include<fstream>
#include<string>
#include<iostream>
#include<opencv2\core\core.hpp>
#include<opencv2\highgui\highgui.hpp>
#include"stdio.h"
using namespace std;
using namespace cv;

int main()
{
	ifstream fin("C:/Users/hxd/Desktop/train_chuli.txt");
	string line;
	string pngPath;
	int label;
	while(getline(fin,line))
	{
	fin >>pngPath;     //文件路径
	fin >>label;       //文件label
//	if(label!=0)
	
	 string s1="E:/hxd_important_file/kaggle_data_zip/train_zip/train.zip/train/"; //文件夹的路径
	 int length=strlen(pngPath.c_str())-strlen(s1.c_str());
	 string name=pngPath.substr(64,length);         //得到文件名,数据根据你的路径长短修改
	 Mat img=imread(pngPath); //加载图像
	 stringstream ss;
	 ss<<label;
	 string slabel=ss.str();
	 const string Path="C:/Users/hxd/Desktop/train_34110/"+slabel+"/"+name;
	 imwrite(Path,img);
	
	}
	
	fin.close();
	return 0;
}

其中txt文档,应该添加属于自己的标签和路径。


本人新手,有什么不好的地方欢迎大家提出一起学习哦


猜你喜欢

转载自blog.csdn.net/qq_36631272/article/details/79192659