C++實現如何對當前目錄下的所有文件進行重命名

#include <iostream>
#include <string>
#include <io.h>
#include <sstream>

using namespace std;

bool RenameFileNames(string strPath, string newPath);

bool RenameFileNames(string oldPath,string newPath)
{
	intptr_t hFile = -1;
	struct _finddata_t fileinfo;
	static int i = 1;

	string path;
	if ((hFile = _findfirst(path.assign(oldPath).append("\\*").c_str(),&fileinfo))!=-1)
	{
		do 
		{
			// 如果當前是目錄,則continue
			if (fileinfo.attrib & _A_SUBDIR)continue;
			else // 對當前文件夾下的文件重命名
			{				
				stringstream ss;
				ss << i;
				
				string new_name = path.assign(newPath).append("\\") + string(ss.str() + ".bmp");
				rename(path.assign(oldPath).append("\\").append(fileinfo.name).c_str(), new_name.c_str());
				i++;
				printf("%s\n", new_name.c_str());
			}
		} while (_findnext(hFile,&fileinfo) == 0);
		_findclose(hFile);
	} 

	return true;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章