c++代码批量修改图片名称(重命名)实例及运行结果

环境vs2013+opencv2.4.9+win10

c++代码

#include <iostream>  
#include <sstream>//格式转换,这里是int转为string
#include <string>  
#include <io.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);//类型转换函数

int main()
{
	_finddata_t file;   // 查找文件的类
	string fileDirectory = "C:\\Users\\2105Share\\Desktop\\trafficSignImage"; //文件夹目录,自己修改
	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); //类型转换
			newFullFilePath = fileDirectory + "\\" + strName + fileType;

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

		} while (_findnext(hFile, &file) == 0);  
		_findclose(hFile);
	}

	system("pause");
	return 0;
}

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();
	}
}

运行结果


  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这里提供一种基于Windows操作系统的C代码示例,可以批量重命名指定目录下的所有.tif格式图片,将它们改名为指定前缀加上自增序号的格式,如"prefix_001.tif", "prefix_002.tif"等。 ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <dirent.h> #include <windows.h> int main() { char* dir_path = "C:\\directory\\to\\images"; //指定目录路径 char* prefix = "new_prefix"; //指定新前缀 int count = 1; //文件计数器,从1开始 int prefix_len = strlen(prefix); char old_file_path[MAX_PATH], new_file_path[MAX_PATH]; struct dirent* entry; DIR* dir = opendir(dir_path); if (dir == NULL) { //目录打开失败 printf("Error: failed to open directory.\n"); return -1; } while ((entry = readdir(dir)) != NULL) { //遍历目录下所有文件 if (entry->d_type == DT_REG && strstr(entry->d_name, ".tif") != NULL) { //只处理.tif格式文件 sprintf(old_file_path, "%s\\%s", dir_path, entry->d_name); sprintf(new_file_path, "%s\\%s_%0*d.tif", dir_path, prefix, prefix_len, count); //生成新文件名 if (rename(old_file_path, new_file_path) != 0) { //重命名失败 printf("Error: failed to rename file %s.\n", entry->d_name); } else { printf("Renamed file %s to %s.\n", entry->d_name, new_file_path); count++; //计数器加1 } } } closedir(dir); return 0; } ``` 上述代码中,使用了Windows系统的文件重命名函数`rename()`,它的参数分别为旧文件名和新文件名,如果重命名成功则返回0,否则返回非零值。新文件名的生成使用了`sprintf()`函数,其中`%s`表示字符串格式,`%0*d`表示补零格式,具体可以参考C语言格式化输出的相关资料。 运行代码时需要将`dir_path`和`prefix`变量修改为实际值,同时需要注意目录路径中的反斜杠`\`需要转义为双反斜杠`\\`,否则会被视为转义字符导致路径错误。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值