我需要把一个文件夹中的照片批量改名,使用rename函数以后,并没有什么效果,有没有人帮我看看是哪出了问题,win11专业版系统。运行结果如图
void AlterName::atler()
{
long long Handle;
cout << this->FilePath << endl;
struct _finddata_t FileInfo;
Handle = _findfirst(this->FilePath, &FileInfo);
cout << Handle << endl;
if (-1 == Handle)
{
std::cout << "Find File Failed!" << std::endl;
return;
}
//因为rename函数在重命名时,传的参数需要指定文件路径,所以需要对新旧文件名称追加路径
std::string imagePath = "../Image/";
std::string oldName = imagePath + FileInfo.name;
//文件名称按照顺序从0还是递增
int ImageIndex = 0;
const char* Prefix = "../Image/%d.jpg";
char newName[100] = { 0 };
snprintf(newName, sizeof(newName), Prefix, ImageIndex);
std::map < std::string, std::string > mapRename;
mapRename.insert(std::pair < std::string, std::string >(oldName, newName));
while (0 == _findnext(Handle, &FileInfo))
{
oldName = imagePath + FileInfo.name;
++ImageIndex;
snprintf(newName, sizeof(newName), Prefix, ImageIndex);
mapRename.insert(std::pair < std::string, std::string >(oldName, newName));
}
for (auto iter = mapRename.begin(); iter != mapRename.end(); ++iter)
{
std::cout << "oldName is " << iter->first.c_str() << std::endl;
std::cout << "newName is " << iter->second.c_str() << std::endl;
//rename(iter->first.c_str(), iter->second.c_str());
}
_findclose(Handle);
//map使用后需要释放,通过swap结合clear
std::map < std::string, std::string > emptyMap;
mapRename.swap(emptyMap);
emptyMap.clear();
}