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

来源:互联网 发布:淘宝c2c商业生态圈 编辑:程序博客网 时间:2024/05/01 22:43

当某一文件夹下的文件类型都一样时,但是文件名乱七八糟,是不是比较难受,至少看起来不是很舒服,那么修改其名字使其顺眼即可,若文件过多,手动改还是很麻烦的,因此,用程序解决比较好。这里用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;}


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

点击打开链接

0 0