[C++] フォルダー内のすべてのファイル名を取得して名前を変更する

1. 機能説明

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

2. フォルダーには他のフォルダーが含まれていません

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 フォルダーにはファイルのみが存在します。ファイル名に「Zhang San 」が含まれるすべてのファイルの名前を「 Li Si 」に変更します

修正する前に:

ここに画像の説明を挿入します

変更後:

ここに画像の説明を挿入します


3. フォルダーには他のフォルダーが含まれています: 反復呼び出し

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 フォルダーには他のフォルダーが含まれており、ファイル名に「Zhang San 」を含むすべてのフォルダー内のファイルの名前が「 Li Si 」に変更されます

修正する前に:

ここに画像の説明を挿入します

ここに画像の説明を挿入します

変更後:

ここに画像の説明を挿入します
ここに画像の説明を挿入します

3. 説明

3.1 FileInfo.attrib ファイルの属性

コード内でif ((FileInfo.attrib & _A_SUBDIR))フォルダーかどうかを判断するために使用されます。

attrib : ファイル属性、ビット表現を表すために使用されます。ファイルに複数の属性がある場合、多くの場合、ビットごとの OR による複数の属性の組み合わせになります。

ファイルのプロパティ 説明する
1 _A_ARCH アーカイブ
2 _A_非表示 隠れる
3 _A_ノーマル 普通
4 _A_RDのみ 読み取り専用
5 _A_SUBDIR フォルダ
6 _システム システム

この記事が参考になったら「いいね!」をお願いします!

ここに画像の説明を挿入します

おすすめ

転載: blog.csdn.net/AAADiao/article/details/131480467