Tabla de contenido
1. Descripción de la función
1. `GetAllFileName()`获取指定路径下所有文件的名称
2. `ReplaceFileName()`替换指定字段,重命名所有符合条件的文件名
2. La carpeta no contiene otras carpetas.
1. Código
#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. Ejemplo
Escenario: solo hay archivos en la carpeta Documento. Cambie el nombre de todos los archivos cuyos nombres contengan " Zhang San " a " Li Si ".
antes de arreglar:
Después de la modificación:
3. La carpeta contiene otras carpetas: llamada iterativa
1. Código
#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. Ejemplo
Escenario: la carpeta Documento contiene otras carpetas y los archivos de todas las carpetas cuyos nombres de archivo contienen " Zhang San " pasan a llamarse " Li Si ".
antes de arreglar:
Después de la modificación:
3. Descripción
3.1 Atributos del archivo FileInfo.attrib
Se utiliza en el código if ((FileInfo.attrib & _A_SUBDIR))
para determinar si es una carpeta.
attrib : se utiliza para representar atributos de archivo, representación de bits. Cuando un archivo tiene múltiples atributos, a menudo es una combinación de varios atributos mediante OR bit a bit.
propiedades de archivo | ilustrar | |
---|---|---|
1 | _A_ARCH | Archivo |
2 | _A_OCULTO | esconder |
3 | _A_NORMAL | normal |
4 | _A_RDONLY | solo lectura |
5 | _A_SUBDIR | carpeta |
6 | _UN SISTEMA | sistema |