C++大量文件转移

1.粗暴定量转移

当遇到特别特别多文件需要从一个文件夹转移到另外一个文件夹,特别是定量的那种,手动真的很累啊很累啊

// ForTransferFlies.cpp : 定义控制台应用程序的入口点。
//

//#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include <string>
#include <string.h>
#include <sys/stat.h>
#include <sstream>
#include <iostream>
#include <fstream>
#include <cstdio>
#include <fstream>
#include<list>
#include <string>
#include <sstream>
#include <vector>
#include<io.h>
#include<stdlib.h> 
using namespace std;
void getAllFiles(string path, vector<string>& files);
typedef std::vector<std::string>  StringList;
StringList splitstr(const std::string& str, char tag);
string&   replace_all(string&   str, const  string&  old_value, const  string&  new_value)
{
    
    
	while (true)
	{
    
    
		string::size_type   pos(0);
		if ((pos = str.find(old_value)) != string::npos)
		{
    
    
			str.replace(pos, old_value.length(), new_value);
		}
		else {
    
     break; }
	}
	return   str;
}
int main()
{
    
    
	string DATA_DIR = "C:\\Users\\wwy\\Desktop\\refSave";//原文件夹
	vector<string> files;
	//测试
	char * DistAll = (char *)"AllFiles.txt";
	getAllFiles(DATA_DIR, files);//所有文件与文件夹的路径都输出
								 //ofstream ofn(DistAll);  //输出文件流
	int size = files.size();//一共多少文件
	int numm = 6416;//想要转移多少文件想要全部转移就numm=size
	int  FaiNum = 0;
	
	for (int i = 0; i < size; i++)
	{
    
    
		/*vector<string> test;
		test = splitstr(files[i], '.');//把读取到的文件名路径进行切割
		if (test[1]=="jpg") {
			cout << files[i] << endl;
		}*/
		if (i <= numm) {
    
    
			cout << "files[i]="<<files[i] << endl;
			//string src = string("D:\\NewForWwy\\workspaceForMine\\DATASET\\VOCofMine\\JPEGImages\\") + "NotVehicle_2" + ".jpg";
			string dst = string("D:\\workspaceForMine\\test\\sharp");//要转移到的文件夹
			replace_all(files[i], "/", "\\");
			cout << "files[2]=" << files[i] << endl;
			string name = "move " + files[i] + " " + dst;
			const char *des_name = name.c_str();
			cout << des_name << endl;
			system(des_name);
		}
       
		
			
		


	}


	/*string src = string("D:\\NewForWwy\\workspaceForMine\\DATASET\\VOCofMine\\JPEGImages\\") + "NotVehicle_2" + ".jpg";
	string dst = string("D:\\NewForWwy\\workspaceForMine\\DATASET\\VOCofMine\\test") ;
	string name = "move " + src + " " + dst;
	const char *des_name = name.c_str();
	cout << des_name << endl;
	system(des_name); //调用系统命令
	cout << "Save !" << endl;*/

	return 0;
}
/*
分割字符串
*/
StringList splitstr(const std::string& str, char tag)
{
    
    
	StringList  li;
	std::string subStr;

	//遍历字符串,同时将i位置的字符放入到子串中,当遇到tag(需要切割的字符时)完成一次切割
	//遍历结束之后即可得到切割后的字符串数组
	for (size_t i = 0; i < str.length(); i++)
	{
    
    
		if (tag == str[i]) //完成一次切割
		{
    
    
			if (!subStr.empty())
			{
    
    
				li.push_back(subStr);
				subStr.clear();
			}
		}
		else //将i位置的字符放入子串
		{
    
    
			subStr.push_back(str[i]);
		}
	}

	if (!subStr.empty()) //剩余的子串作为最后的子字符串
	{
    
    
		li.push_back(subStr);
	}

	return li;
}
/*
读取文件名
*/
void getAllFiles(string path, vector<string>& files)
{
    
    
	//文件句柄  
	intptr_t hFile = 0;
	//文件信息  
	struct _finddata_t fileinfo;  //很少用的文件信息读取结构
	string p;  //string类很有意思的一个赋值函数:assign(),有很多重载版本
	if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
	{
    
    
		do
		{
    
    
			if ((fileinfo.attrib &  _A_SUBDIR))  //判断是否为文件夹
			{
    
    
				if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
				{
    
    
					files.push_back(p.assign(path).append("/").append(fileinfo.name));//保存文件夹名字
					getAllFiles(p.assign(path).append("/").append(fileinfo.name), files);//递归当前文件夹
				}
			}
			else    //文件处理
			{
    
    
				files.push_back(p.assign(path).append("/").append(fileinfo.name));//文件名
			}
		} while (_findnext(hFile, &fileinfo) == 0);  //寻找下一个,成功返回0,否则-1
		_findclose(hFile);
	}
}



2.把大量文件定量转移到多个文件夹

定量转移到这样的多个文件夹,但顺序是乱的,顺序为1,10,11,101…
在这里插入图片描述

//#include <iostream>
//using namespace std;
//
//int main()
//{
    
    
//	string folderPath = "C:\\Users\\大喵喵\\Desktop\\大气湍流\\turbulence_dataset\\algorithm_simulated_videos\\blur\\00";
//
//	string command;
//	command = "mkdir -p " + folderPath;
//	system(command.c_str());
//
//	return 0;
//}



// ForTransferFlies.cpp : 定义控制台应用程序的入口点。
//

//#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include <string>
#include <string.h>
#include <sys/stat.h>
#include <sstream>
#include <iostream>
#include <fstream>
#include <cstdio>
#include <fstream>
#include<list>
#include <string>
#include <sstream>
#include <vector>
#include<io.h>
#include<stdlib.h> 
using namespace std;
void getAllFiles(string path, vector<string>& files);
typedef std::vector<std::string>  StringList;
StringList splitstr(const std::string& str, char tag);
string&   replace_all(string&   str, const  string&  old_value, const  string&  new_value)
{
    
    
	while (true)
	{
    
    
		string::size_type   pos(0);
		if ((pos = str.find(old_value)) != string::npos)
		{
    
    
			str.replace(pos, old_value.length(), new_value);
		}
		else {
    
     break; }
	}
	return   str;
}
int main()
{
    
    
	string DATA_DIR = "C:\\Users\\大喵喵\\Desktop\\大气湍流\\turbulence_dataset\\algorithm_simulated_videos\\inputFramesPNG\\1";//原文件夹
	vector<string> files;
	//测试
	char * DistAll = (char *)"AllFiles.txt";
	getAllFiles(DATA_DIR, files);//所有文件与文件夹的路径都输出
								 //ofstream ofn(DistAll);  //输出文件流
	int size = files.size();//一共多少文件
	int numm = 100;//每个文件夹定量多少
	int  FaiNum = 0;
	int k = size / numm;
	/*for (int j = 0; j < k; j++) {*/
		    
			for (int i = 0; i < k; i++)
			{
    
    
				string folderPath;
				string j_str = to_string(i);
				folderPath = "C:\\Users\\大喵喵\\Desktop\\大气湍流\\turbulence_dataset\\algorithm_simulated_videos\\blur\\" + j_str+"\\Blur\\RGB";//创建新文件夹路径
				const char* f1 = folderPath.c_str();
				if (_access(f1, 0) == -1) {
    
    
					string command;
					command = "mkdir -p " + folderPath;
					system(command.c_str());
													
					for (int w = numm * i; w < numm * i + numm; w++) {
    
    
						cout << "files[i]=" << files[w] << endl;
						//string src = string("D:\\NewForWwy\\workspaceForMine\\DATASET\\VOCofMine\\JPEGImages\\") + "NotVehicle_2" + ".jpg";
						string dst = string(folderPath);//要转移到的文件夹
						replace_all(files[w], "/", "\\");
						cout << "files[2]=" << files[w] << endl;
						string name = "move " + files[w] + " " + dst;
						const char *des_name = name.c_str();
						cout << des_name << endl;
						system(des_name);
					}
				}
			}
	/*}*/
	
	return 0;
}
/*
分割字符串
*/
StringList splitstr(const std::string& str, char tag)
{
    
    
	StringList  li;
	std::string subStr;

	//遍历字符串,同时将i位置的字符放入到子串中,当遇到tag(需要切割的字符时)完成一次切割
	//遍历结束之后即可得到切割后的字符串数组
	for (size_t i = 0; i < str.length(); i++)
	{
    
    
		if (tag == str[i]) //完成一次切割
		{
    
    
			if (!subStr.empty())
			{
    
    
				li.push_back(subStr);
				subStr.clear();
			}
		}
		else //将i位置的字符放入子串
		{
    
    
			subStr.push_back(str[i]);
		}
	}

	if (!subStr.empty()) //剩余的子串作为最后的子字符串
	{
    
    
		li.push_back(subStr);
	}

	return li;
}
/*
读取文件名
*/
void getAllFiles(string path, vector<string>& files)
{
    
    
	//文件句柄  
	intptr_t hFile = 0;
	//文件信息  
	struct _finddata_t fileinfo;  //很少用的文件信息读取结构
	string p;  //string类很有意思的一个赋值函数:assign(),有很多重载版本
	if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
	{
    
    
		do
		{
    
    
			if ((fileinfo.attrib &  _A_SUBDIR))  //判断是否为文件夹
			{
    
    
				if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
				{
    
    
					files.push_back(p.assign(path).append("/").append(fileinfo.name));//保存文件夹名字
					getAllFiles(p.assign(path).append("/").append(fileinfo.name), files);//递归当前文件夹
				}
			}
			else    //文件处理
			{
    
    
				files.push_back(p.assign(path).append("/").append(fileinfo.name));//文件名
			}
		} while (_findnext(hFile, &fileinfo) == 0);  //寻找下一个,成功返回0,否则-1
		_findclose(hFile);
	}
}




3.opencv 把大量文件定量顺序转移到多个文件夹

①需要opencv库
②文件名需要为纯数字,例如:1.jpg、2.jpg…

#include "stdio.h"
#include "stdlib.h"
#include <string>
#include <string.h>
#include <sys/stat.h>
#include <sstream>
#include <iostream>
#include <fstream>
#include <cstdio>
#include <fstream>
#include<list>
#include <string>
#include <sstream>
#include <vector>
#include<io.h>
#include<stdlib.h> 
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/ml.hpp"
#include "opencv2/objdetect.hpp"
#include <string>
using namespace std;
vector<string> getOrderedNames(vector<cv::String> filenames, cv::String folder) {
    
    
	vector<string> nameArr;
	string  suffixName;
	for (size_t i = 0; i < filenames.size(); ++i)
	{
    
    
		string::size_type iPos = filenames[i].find_last_of('\\') + 1;
		string filename = filenames[i].substr(iPos, filenames[i].length() - iPos);
		string name = filename.substr(0, filename.rfind("."));
		suffixName = filename.substr(filename.rfind("."), filename.length() - 1);//获取后缀名
		nameArr.emplace_back(name);
	}
	sort(nameArr.begin(), nameArr.end(), [](string a, string b) {
    
    return stoi(a) < stoi(b); });

	vector<string> orderedFilenames;
	for (size_t i = 0; i < filenames.size(); ++i)
	{
    
    

		string orderedName = folder + "\\" + nameArr[i] + suffixName;
		orderedFilenames.emplace_back(orderedName);
		//cout << "改变后:" << orderedFilenames[i] << endl;
	}
	return orderedFilenames;
}
typedef std::vector<std::string>  StringList;
StringList splitstr(const std::string& str, char tag);
string&   replace_all(string&   str, const  string&  old_value, const  string&  new_value)
{
    
    
	while (true)
	{
    
    
		string::size_type   pos(0);
		if ((pos = str.find(old_value)) != string::npos)
		{
    
    
			str.replace(pos, old_value.length(), new_value);
		}
		else {
    
     break; }
	}
	return   str;
}
int main()
{
    
    
	std::vector<cv::String> filenames; // notice here that we are using the Opencv's embedded "String" class
	cv::String folder = "C:\\Users\\大喵喵\\Desktop\\大气湍流\\turbulence_dataset\\algorithm_simulated_videos\\refFramesPNG"; // 被转移文件所在文件夹
	cv::glob(folder, filenames); // new function that does the job ;-)
	size_t size = filenames.size();
	if (size == 0)
	{
    
    
		std::cout << "file " << folder << " not  exits" << std::endl;
		return -1;
	}

	vector<string> orderedFilenames = getOrderedNames(filenames, folder);

	/*for (size_t i = 0; i < filenames.size(); ++i)
	{
		cout << "顺序:" << orderedFilenames[i] << endl;
	}*/
	int numm = 100;//每个文件夹定量多少
	int  FaiNum = 0;
	int k = size / numm;
	/*for (int j = 0; j < k; j++) {*/

	for (int i = 0; i < k; i++)
	{
    
    
		string folderPath;
		string j_str = to_string(i);
		folderPath = "C:\\Users\\大喵喵\\Desktop\\大气湍流\\turbulence_dataset\\algorithm_simulated_videos\\trainBSD\\" + j_str + "\\Sharp\\RGB";//创建文件夹位置
		const char* f1 = folderPath.c_str();
		if (_access(f1, 0) == -1) {
    
    
			string command;
			command = "mkdir -p " + folderPath;
			system(command.c_str());

			for (int w = numm * i; w < numm * i + numm; w++) {
    
    
				cout << "要转移的文件:" << orderedFilenames[w] << endl;
				//string src = string("D:\\NewForWwy\\workspaceForMine\\DATASET\\VOCofMine\\JPEGImages\\") + "NotVehicle_2" + ".jpg";
				string dst = string(folderPath);//要转移到的文件夹
				replace_all(orderedFilenames[w], "/", "\\");
				//cout << "files[2]=" << orderedFilenames[w] << endl;
				string name = "move " + orderedFilenames[w] + " " + dst;
				const char *des_name = name.c_str();
				cout << des_name << endl;
				system(des_name);
			}
		}
	}

}

猜你喜欢

转载自blog.csdn.net/Cream_Cicilian/article/details/124524084
今日推荐