[C++] Get all file names in a folder and rename them

1. Function description

	1. `GetAllFileName()`获取指定路径下所有文件的名称
	2. `ReplaceFileName()`替换指定字段,重命名所有符合条件的文件名

2. The folder does not contain other folders

1. Code

#include <iostream>
#include <io.h>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

int GetAllFileName(const string RootPath, vector<string>& FilePathNames)
{
    
    
	string EachPath = RootPath;      //临时路径,用于字符拼接
	intptr_t FileHandle;             //文件句柄
	struct _finddata_t FileInfo;     //文件信息

	if ((FileHandle = _findfirst(EachPath.append("\\*").c_str(), &FileInfo)) == -1)
	{
    
    
		cout << "未找到文件! " << endl;
		return -1;
	}
	else
	{
    
    
		while (_findnext(FileHandle, &FileInfo) == 0)
		{
    
    
			if (strcmp(FileInfo.name, ".") != 0 && strcmp(FileInfo.name, "..") != 0)
			{
    
    
				FilePathNames.push_back(FileInfo.name);
				cout << "FileName : " << FileInfo.name << endl;
			}
		}
		_findclose(FileHandle);
	}
	return 0;
}

int ReplaceFileName(const string oldFileName, string ModifyBeforeFilds, string ModifyAfterFilds)
{
    
    
    //临时文件名用于字符替换
	string newFileName = oldFileName;

	int findIndex = newFileName.find(ModifyBeforeFilds, 1);
	if (findIndex != -1)
	{
    
    
		newFileName = newFileName.replace(findIndex, ModifyBeforeFilds.size(), ModifyAfterFilds);
	}

	fstream fs;
	fs.open(oldFileName.c_str());
	if (fs.fail())
	{
    
    
		cout << "文件打开失败!" << endl;
		fs.close();
		return -1;
	}
	else
	{
    
    
		fs.close();
		if (rename(oldFileName.c_str(), newFileName.c_str()) == -1)   //文件重命名
		{
    
    
			cout << "文件名修改失败!" << endl;
			return -1;
		}
		return 0;
	}
}

int main()
{
    
    
	string Path = "C:\\Users\\Adiao\\Desktop\\Document";      //自定义路径
	vector<string> FilePathNames;                      //存放所有文件的路径名称
	string ModifyBeforeFilds = "张三";                 //指定修改前的字段
	string ModifyAfterFilds = "李四";                  //指定修改后的字段
	
	//获取所有文件名路径
	GetAllFileName(Path, FilePathNames);
	cout << "该路径下文件总数为:" << FilePathNames.size() << endl;
	for (auto name : FilePathNames)
	{
    
    
		string CompletePath = Path + "\\" + name;
		int ret = ReplaceFileName(CompletePath, ModifyBeforeFilds, ModifyAfterFilds);
		if (ret == 0)
		{
    
    
			cout << "文件重命名完毕!" << endl;
		}
	}

	system("pause");
	return 0;
}

2. Example

Scenario: There are only files in the folder Document. Rename all files whose file names contain " Zhang San " to " Li Si ".

before fixing:

Insert image description here

After modification:

Insert image description here


3. The folder contains other folders: iterative call

1. Code

#include <iostream>
#include <io.h>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

int GetAllFileName(const string RootPath, vector<string>& FilePathNames)
{
    
    
	string EachPath = RootPath;      //临时路径,用于字符拼接
	intptr_t FileHandle;             //文件句柄
	struct _finddata_t FileInfo;     //文件信息

	//使用_findfirst查找文件,获取文件信息
	if ((FileHandle = _findfirst(EachPath.append("\\*").c_str(), &FileInfo)) == -1)
	{
    
    
		cout << "未找到文件! " << endl;
		return -1;
	}
	else
	{
    
    
		do
		{
    
       // 比较文件类型是否是文件夹
			if ((FileInfo.attrib &  _A_SUBDIR))
			{
    
    
				if (strcmp(FileInfo.name, ".") != 0 && strcmp(FileInfo.name, "..") != 0)
				{
    
    
					EachPath = RootPath;   //每次从根路径拼接
					//递归调用
					GetAllFileName(EachPath.append("\\").append(FileInfo.name), FilePathNames);
				}
			}
			else
			{
    
    
				EachPath = RootPath;
				FilePathNames.push_back(EachPath.append("\\").append(FileInfo.name));

				cout << "FileName : " << FileInfo.name << endl;
			}
		} while (_findnext(FileHandle, &FileInfo) == 0);
		_findclose(FileHandle);
	}
	return 0;
}

int ReplaceFileName(const string OldFileName, string ModifyBeforeFilds, string ModifyAfterFilds)
{
    
    
	string NewFileName = OldFileName;
	//查找指定字段
	int findIndex = NewFileName.find(ModifyBeforeFilds, 1);
	if (findIndex != -1)
	{
    
    
		NewFileName = NewFileName.replace(findIndex, ModifyBeforeFilds.size(), ModifyAfterFilds);
	}

	fstream fs;
	fs.open(OldFileName.c_str());
	if (fs.fail())
	{
    
    
		cout << "文件打开失败!" << endl;
		fs.close();
		return -1;
	}
	else
	{
    
    
		fs.close();
		if (rename(OldFileName.c_str(), NewFileName.c_str()) == -1)   //文件重命名
		{
    
    
			cout << "文件名修改失败!" << endl;
			return -1;
		}
		return 0;
	}
}

int main()
{
    
    
	string Path = "C:\\Users\\Adiao\\Desktop\\Document";   //自定义路径
	string ModifyBeforeFilds = "张三";                     //指定修改前的字段
	string ModifyAfterFilds = "李四";                      //指定修改后的字段
	vector<string> FilePathNames;    //存放所有文件的路径名称

	//获取所有文件名路径
	GetAllFileName(Path, FilePathNames);

	cout << "该路径下文件总数为:" << FilePathNames.size() << endl;
	for (auto PathName : FilePathNames)
	{
    
    
		int ret = ReplaceFileName(PathName, ModifyBeforeFilds, ModifyAfterFilds);
		if (ret == 0)
		{
    
    
			cout << "文件重命名完毕!" << endl;
		}
	}

	system("pause");
	return 0;
}

2. Example

Scenario: The folder Document contains other folders, and the files in all folders whose file names contain " Zhang San " are renamed to " Li Si ".

before fixing:

Insert image description here

Insert image description here

After modification:

Insert image description here
Insert image description here

3. Description

3.1 FileInfo.attrib file attributes

Used in the code if ((FileInfo.attrib & _A_SUBDIR))to determine whether it is a folder.

attrib : used to represent file attributes, bit representation. When a file has multiple attributes, it is often a combination of several attributes through bitwise OR.

file properties illustrate
1 _A_ARCH Archive
2 _A_HIDDEN hide
3 _A_NORMAL normal
4 _A_RDONLY read only
5 _A_SUBDIR folder
6 _A_SYSTEM system

If this article is helpful to you, I would like to receive a like from you!

Insert image description here

Guess you like

Origin blog.csdn.net/AAADiao/article/details/131480467