两个文件夹互相找对方的同名文件

// ForTransferFlies.cpp : 定义控制台应用程序的入口点。
//
/***
	  Creat By:大喵喵
	  Description:两个文件夹里面的文件,均是同名一一对应的(数据集的图片和标注),本脚本是保证两个文件夹确实是一一对应
	  有点粗糙,详细注意点在下面写明了①②③④⑤

***/
#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 = "D:/workspaceForMine/ForYoloV2/DataSet/DataSet6/xml";//①根据该文件夹的文件名去查找
	vector<string> files;
	//测试
	char * DistAll = (char *)"AllFiles.txt";
	getAllFiles(DATA_DIR, files);//所有文件与文件夹的路径都输出
								 //ofstream ofn(DistAll);  //输出文件流
	int size = files.size();
	int  FaiNum = 0;
	//ofn << size << endl;
	//cout << size << endl;
	for (int i = 0; i < size; i++)
	{
		vector<string> test;
		test = splitstr(files[i], '/');//把读取到的文件名路径进行切割
		string picName = test[test.size() - 1];
		picName = picName.substr(0, picName.length() - 4);//④减去后缀名,注意.jpg是4个字符所以这里是4,如果你的后缀名不是4个字符,此处需要修改
		cout << picName << endl;//单纯的名字
        string src = string("D:\\workspaceForMine\\ForYoloV2\\DataSet\\DataSet6\\images\\") + picName+ ".png";//②在这个文件夹找到同名文件
		                                                                                               //③这个后缀名根据实际情况进行修改
	    string dst = string("D:\\workspaceForMine\\ForYoloV2\\DataSet\\DataSet6\\imgs\\");//③转移到的文件夹
			replace_all(files[i], "/", "\\");
			string name = "move " + src + " " + 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);
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值