批量提取文件夾中文件名(C++代碼實現)

批量獲取某一個文件中的文件名稱,C++代碼測試可用,實現了當前文件夾中所有文件的文件名讀取,包括“文件路徑+文件名+文件擴展名”,不包括子文件夾名,讀取文件名在cmd終端顯示,並保存在一個fileName.txt文件中。

C++代碼如下:

#if 1
#include<iostream>
#include<vector>
#include<time.h>
#include<io.h>
#include <fstream>  //文件操作
using namespace std;


//for reading file
void getFiles( string path, vector<string>& files );
void getFilesName( string path, vector<string>& files );
char* str2char(string src) ;

int main()
{
	//測試文件路徑
	char path1[300]="C:\\Users\\admin\\Pictures";

	vector<string> filenames1;
	getFiles( path1, filenames1);

	vector<string> filenames2;
	getFilesName(path1, filenames2 );

	//新建一個.txt文件,用於保存文件名
	ofstream ofn("FileName.txt");  

	int numImg=filenames2.size();
	for (int file_i=0;file_i<numImg;  file_i++)
	{
		string b1 =filenames1[file_i].c_str();
		string b2 =filenames2[file_i].c_str();

		char * test_b1=str2char(b1);
		char * testChar=str2char(b2);


		char noSuffixName[100];
		//僅讀取文件名,不包括文件擴展名,如文件名爲0_0_272.jpg,則讀取文件名noSuffixName="0_0_272"
		int	indexPoint = b2.find_first_of('.',0); 
		//int	indexPointEnd = b.find_first_of('.',0); 
		for(int j=0;j<indexPoint;j++){
			noSuffixName[j]=testChar[j];
		}
		noSuffixName[indexPoint]='\0';//輸入結束符

		//顯示提取的文件名稱
		cout<<"noSuffixName:"<<noSuffixName<<endl;//文件名稱 (不包括文件擴展名)
		cout<<"fileName:"<<testChar<<endl;//文件名稱+文件擴展名
		cout<<"pathFileName:"<<test_b1<<endl;//路徑+文件名稱+文件擴展名
		//將文件名稱保存到.txt文件
		ofn<<"noSuffixName:"<<noSuffixName<<endl;   
		ofn<<"fileName:"<<testChar<<endl;
		ofn<<"pathFileName:"<<test_b1<<endl;

 	    system("pause");//程序暫停,按任意鍵繼續

	}

	ofn.close(); 


return 0;

}


void getFiles( string path, vector<string>& files )
{
	//文件句柄
	long   hFile   =   0;
	//文件信息
	struct _finddata_t fileinfo;
	string p;
	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)
					getFiles( p.assign(path).append("\\").append(fileinfo.name), files );
			}
			else
			{
				files.push_back(p.assign(path).append("\\").append(fileinfo.name) );
			}
		}while(_findnext(hFile, &fileinfo)  == 0);
		_findclose(hFile);
	}
}

void getFilesName( string path, vector<string>& files )
{
	//文件句柄
	long   hFile   =   0;
	//文件信息
	struct _finddata_t fileinfo;
	string p;
	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)
					//getFiles( p.assign(path).append("\\").append(fileinfo.name), files );
						getFiles( fileinfo.name, files );
			}
			else
			{
				//files.push_back(p.assign(path).append("\\").append(fileinfo.name) );
				files.push_back(fileinfo.name);
			}
		}while(_findnext(hFile, &fileinfo)  == 0);
		_findclose(hFile);
	}
}

char* str2char(string src) 
{
	char *dst = new char[255];
	int i; 
	for(i=0;i <=src.length();i++) 
		dst[i]=src[i]; 
	dst[i] = '\0';
	return dst;
}


#endif 



發佈了17 篇原創文章 · 獲贊 62 · 訪問量 21萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章