可根据需求进行相应修改,实现各种需求的文件名更改
#include <stdio.h>#include <iostream>#include <string>#include <string.h>#include <sstream>#include <io.h>using namespace std;void reNameFiles(string path){//文件句柄long hFile = 0;//文件信息struct _finddata_t fileinfo;static int i = 1;string p;if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1){do{//如果是目录,迭代if ((fileinfo.attrib & _A_SUBDIR)){if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)reNameFiles(p.assign(path).append("\\").append(fileinfo.name));}//如果不是,重命名else{string temp = "";string fileName =temp.append(fileinfo.name).c_str();//获取文件名的后缀string suffixName = fileName.substr(fileName.rfind(".") + 1, path.length());//若目标后缀是vtt 进行重命名if (suffixName == "vtt"){//获取不带后缀的文件名string noSuffixName = fileName.substr(0, fileName.rfind("."));string new_name = p.assign(path).append("\\") + noSuffixName + ".srt";rename(p.assign(path).append("\\").append(fileinfo.name).c_str(), new_name.c_str());}}} while (_findnext(hFile, &fileinfo) == 0);_findclose(hFile);}}int main(){string path = "F:\\UE4\\教程\\Unreal Engine 4 Mastery:Create Multiplayer Games with C++";reNameFiles(path);return 0;}