// rename.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<Windows.h>
#include<string>
#include<iostream>
#include<io.h>
#include<fstream>
#include<stdio.h>
using namespace std;
#define npos 4294967295
WIN32_FIND_DATA fd;
string filename[99999]={""};
int allFiles=-1;
string myDir="";
char dir[1024]={0};
string path1=myDir+"*.jpeg";
//不包括港澳台
string province="京津沪渝蒙桂宁藏新浙苏赣皖闽粤贵鄂湘川云琼鲁晋冀豫陕甘青辽吉黑NS"/*,"台港澳"*/;
HANDLE hFind;
string newName="";
string name="";
std::fstream fs;
struct _finddata_t files;
int success=0,faild=0;
int _tmain(int argc, _TCHAR* argv[])
{
//gets(dir);
//myDir="C:\\Users\\ljq\\Documents\\visual studio 2012\\Projects\\rename\\rename\\tmp\\";
myDir="C:\\Users\\ljq\\Desktop\\未改动\\";
path1=myDir+"*.jpeg";
hFind = ::FindFirstFile(path1.c_str(), &fd);
if (hFind == INVALID_HANDLE_VALUE)
return 0;
do
{
string name=fd.cFileName;
cout<<name<<endl;
allFiles++;
filename[allFiles]=name;
}
while (::FindNextFile(hFind, &fd));
::FindClose(hFind);
int nowPos=0;
for(nowPos=0;nowPos<=allFiles;nowPos++)
{
name=filename[nowPos];
int len=name.length();
// 省份位置
size_t pos=-1;
for(int k=0;k<province.length();k+=2)
{
string prov=province.substr(k,2);
pos=name.find(prov);
if(pos>=0&&pos<name.length())
break;
}
if(pos<0||pos>=name.length())
continue;
// 是否有特殊字
size_t pos_spe=8;
if(name.find("挂")!=npos||name.find("学")!=npos||name.find("警")!=npos)
{
pos_spe=9;
}
//新名字
newName=name.substr(pos,pos_spe);
cout<<newName<<endl;
//name="C:\\Users\\ljq\\Desktop\\未改动\\"+name;
name =myDir+name;
//newName="C:\\Users\\ljq\\Desktop\\未改动\\"+newName;
newName=myDir+newName;
//重命名
int retry=1;
if(rename(name.c_str(),(newName+".jpeg").c_str())==-1)
{
while(rename(name.c_str(),(newName+"("+to_string(retry)+")"+".jpeg").c_str())==-1)
{
faild++;
retry++;
cout<<"renamefalse"<<endl;
}
}
success++;
}
cout<<"faild:"<<faild<<endl<<"success:"<<success;
return 0;
}
这是用来更改图片名称的