批量修改图片名 C++

#include <iostream>
#include<Windows.h>
#include <io.h>  //进行系统文件操作的头文件  
#include <sstream>//格式转换,这里是int转为string  
#include <string>
#include<TCHAR.h>


using namespace std;
const int N = 3;//假如少于100张,N=2,则编号1~99;N=3,则编号01~099,N=4,则编号0001~0099  
const string fileType = ".png";//需要重命名图片的格式  
string int2string(int n, int i);//类型转换函数
string GetExePath();//获取当前exe的路径
void string_replace(string &strBig, const string &strsrc, const string &strdst);
void HandleFile(string FileName, string strSrc, string strDst);


int main(int argc,char *argv[])
{
cout << endl << "argc = " << argc << endl;
cout << "Command line arguments received are:" << endl;
for (int i = 0; i < argc; i++)
{
cout << "argument " << (i + 1) << ": " << argv[i] << endl;
}
string FileName = argv[1];;//存放图片的文件夹
string strSrc = argv[2];//被替换的字符串
string strDst = argv[3];//替换的字符串
HandleFile(FileName, strSrc, strDst);


system("pause");
return 0;
}






string GetExePath()
{
char szFilePath[MAX_PATH + 1] = { 0 };
GetModuleFileNameA(NULL, szFilePath, MAX_PATH);
(strrchr(szFilePath, '\\'))[0] = 0; // 删除文件名,只获得路径字串  
string strPath ;


for (int n = 0; szFilePath[n]; n++)
{
if (szFilePath[n] != _T('\\'))
{
strPath += szFilePath[n];
}
else
{
strPath += _T("\\");
}
}
return strPath;


}


void string_replace(string &strBig, const string &strsrc, const string &strdst)
{
string::size_type pos = 0;
string::size_type srclen = strsrc.size();
string::size_type dstlen = strdst.size();


while ((pos = strBig.find(strsrc, pos)) != std::string::npos)
{
strBig.replace(pos, srclen, strdst);
pos += dstlen;
}
}


string int2string(int n, int i)//类型转换函数  
{
char s[BUFSIZ];
sprintf_s(s, "%d", i);
int len = strlen(s);
if (len > n)
{
cout << "输入的N太小!";
}
else
{
stringstream Num;
for (int i = 0; i < n - len; i++)
Num << "0";
Num << i;


return Num.str();
}
}


void HandleFile(string FileName, string strSrc, string strDst)
{
_finddata_t file;   // 查找文件的类  
string fileDirectory = GetExePath()+"\\" + FileName;//当前程序路径
string buffer = fileDirectory +"\\*" + fileType;
//long hFile;//win7系统  
intptr_t hFile;//win10系统  
hFile = _findfirst(buffer.c_str(), &file); //找第一个文件  


if (hFile == -1L)
cout << "没有指定格式的图片" << endl;//没有指定格式的图片  
else
{
int i = 0;
string newFullFilePath;
string oldFullFilePath;
string strName;
do
{
oldFullFilePath.clear();
newFullFilePath.clear();
strName.clear();


oldFullFilePath = fileDirectory + "\\" + file.name;
//++i;
//strName = int2string(N, i); //类型转换
string strName = file.name;
string_replace(strName, strSrc, strDst);//替换字符串
newFullFilePath = fileDirectory + "\\" + strName;


int c = rename(oldFullFilePath.c_str(), newFullFilePath.c_str());//重命名  
if (c == 0)
cout << "重命名成功" << strName << endl;
else
cout << "重命名失败" << strName << endl;


} while (_findnext(hFile, &file) == 0);
_findclose(hFile);
}
return;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值