您的位置:首页 > 编程语言 > C语言/C++

C++实现某文件夹下文件重命名

2014-12-15 19:11 253 查看
当某一文件夹下的文件类型都一样时,但是文件名乱七八糟,是不是比较难受,至少看起来不是很舒服,那么修改其名字使其顺眼即可,若文件过多,手动改还是很麻烦的,因此,用程序解决比较好。这里用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)
getFiles( p.assign(path).append("\\").append(fileinfo.name));
}
//如果不是,重命名
else
{
//cout<<fileinfo.name<<endl;
stringstream ss;
ss<<i;
i++;
string new_name = p.assign(path).append("\\")+string(ss.str()+".jpg");
//cout<<new_name<<endl;
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;
cin >> path;
reNameFiles(path);
return 0;
}


这个程序是从得到某文件夹下的所有文件名字而来。

点击打开链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: