【C++】获取文件夹下所有文件名并重命名

一、函数功能说明

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

二、文件夹中不包含其他文件夹

1. 代码

#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. 示例

场景:文件夹Document下只有文件,将所有文件名包含”张三“的文件重命名为”李四“。

修改前:

在这里插入图片描述

修改后:

在这里插入图片描述


三、文件夹中包含其他文件夹:迭代调用

1. 代码

#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. 示例

场景:文件夹Document下包含其他文件夹,将所有文件夹中文件名包含”张三“的文件重命名为”李四“。

修改前:

在这里插入图片描述

在这里插入图片描述

修改后:

在这里插入图片描述
在这里插入图片描述

3. 说明

3.1 FileInfo.attrib文件属性

代码中使用if ((FileInfo.attrib & _A_SUBDIR))来判断是否为文件夹。

attrib:用于表示文件的属性,位表示,当一个文件有多个属性时,它往往是通过位或的方式,来得到几个属性的综合。

文件属性 说明
1 _A_ARCH 存档
2 _A_HIDDEN 隐藏
3 _A_NORMAL 正常
4 _A_RDONLY 只读
5 _A_SUBDIR 文件夹
6 _A_SYSTEM 系统

如果这篇文章对你有所帮助,渴望获得你的一个点赞!

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/AAADiao/article/details/131480467