1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 【C++】批量修改路径下所有文件夹中的文件后缀名

【C++】批量修改路径下所有文件夹中的文件后缀名

时间:2019-04-09 23:34:40

相关推荐

【C++】批量修改路径下所有文件夹中的文件后缀名

可根据需求进行相应修改,实现各种需求的文件名更改

#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;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。