Windows下对文件夹下所有图片批量重命名(附C++,python,matlab代码)
https://blog.csdn.net/u011574296/article/details/72956446:
Windows下对文件夹下所有图片批量重命名(附C++,python,matlab代码)
原文件夹
重命名之后
C++
#include <iostream>
#include <io.h> //对系统文件进行操作的头文件
#include <string>
#include <sstream>
#include<vector>
using namespace std;
const int N = 6; //整型格式化输出为字符串后的长度,例如,N=6,则整型转为长度为6的字符串,12转为为000012
const string FileType = ".jpg"; // 需要查找的文件类型
/* 函数说明 整型转固定格式的字符串
输入:
n 需要输出的字符串长度
i 需要结构化的整型
输出:
返回转化后的字符串
*/
string int2string(int n, int i)
{
char s[BUFSIZ];
sprintf(s, "%d", i);
int l = strlen(s); // 整型的位数
if (l > n)
{
cout << "整型的长度大于需要格式化的字符串长度!";
}
else
{
stringstream M_num;
for (int i = 0;i < n - l;i++)
M_num << "0";
M_num << i;
return M_num.str();
}
}
int main()
{
_finddata_t c_file; // 查找文件的类
string File_Directory ="E:\\image"; //文件夹目录
string buffer = File_Directory + "\\*" + FileType;
//long hFile; //win7系统,_findnext()返回类型可以是long型
intptr_t hFile; //win10系统 ,_findnext()返回类型为intptr_t ,不能是long型
hFile = _findfirst(buffer.c_str(), &c_file); //找第一个文件
if (hFile == -1L) // 检查文件夹目录下存在需要查找的文件
printf("No %s files in current directory!\n", FileType);
else
{
printf("Listing of files:\n");
int i = 0;
string newfullFilePath;
string oldfullFilePath;
string str_name;
do
{
oldfullFilePath.clear();
newfullFilePath.clear();
str_name.clear();
//旧名字
oldfullFilePath = File_Directory + "\\" + c_file.name;
//新名字
++i;
str_name = int2string(N, i); //整型转字符串
newfullFilePath = File_Directory + "\\"+ str_name + FileType;
/*重命名函数rename(const char* _OldFileName,const char* _NewFileName)
第一个参数为旧文件路径,第二个参数为新文件路径*/
int c = rename(oldfullFilePath.c_str(), newfullFilePath.c_str());
if (c == 0)
puts("File successfully renamed");
else
perror("Error renaming file");
} while (_findnext(hFile, &c_file) == 0); //如果找到下个文件的名字成功的话就返回0,否则返回-1
_findclose(hFile);
}
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
将整型格式化输出到字符串中,我是使用了stringstream类,写了一个函数实现这个功能,还有一种实现方式是,使用sprintf将数字转为字符串,同时可以格式化字符串。如下:
char s[50];
int i = 1;
sprintf(s,”%06d”, i); // 将整型i转为字符串s,指定宽度为6,不足的左边补0
sprintf 的更多用法,可以参考博客:C++字符串格式化 sprintf、printf
/* 函数说明 整型转固定格式的字符串
输入:
n 需要输出的字符串长度
i 需要结构化的整型
输出:
返回转化后的字符串
*/
string int2string(int n, int i)
{
char s[BUFSIZ];
sprintf(s, "%d", i);
int l = strlen(s); // 整型的位数
if (l > n)
{
cout << "整型的长度大于需要格式化的字符串长度!";
}
else
{
stringstream M_num;
for (int i = 0;i < n - l;i++)
M_num << "0";
M_num << i;
return M_num.str();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
python
import os
path = "E:\\image"
filelist = os.listdir(path) #该文件夹下所有的文件(包括文件夹)
count=0
for file in filelist:
print(file)
for file in filelist: #遍历所有文件
Olddir=os.path.join(path,file) #原来的文件路径
if os.path.isdir(Olddir): #如果是文件夹则跳过
continue
filename=os.path.splitext(file)[0] #文件名
filetype=os.path.splitext(file)[1] #文件扩展名
Newdir=os.path.join(path,str(count).zfill(6)+filetype) #用字符串函数zfill 以0补全所需位数
os.rename(Olddir,Newdir)#重命名
count+=1
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
matlab
%%
%图片保存路径为:
%E:\image\car
%E:\image\person
%car和person是保存车和行人的文件夹
%这些文件夹还可以有多个,
%放在image文件夹里就行
%该代码的作用是将图片名字改成000123.jpg这种形式
%%
clc;
clear;
maindir='E:\image\';
name_long=6; %图片名字的长度,如000123.jpg为6,最多9位,可修改
num_begin=1; %图像命名开始的数字如000123.jpg开始的话就是123
subdir = dir(maindir);
n=1;
for i = 1:length(subdir)
if ~strcmp(subdir(i).name ,'.') && ~strcmp(subdir(i).name,'..')
subsubdir = dir(strcat(maindir,subdir(i).name));
for j=1:length(subsubdir)
if ~strcmp(subsubdir(j).name ,'.') && ~strcmp(subsubdir(j).name,'..')
img=imread([maindir,subdir(i).name,'\',subsubdir(j).name]);
imshow(img);
str=num2str(num_begin,'%09d');
newname=strcat(str,'.jpg');
newname=newname(end-(name_long+3):end);
system(['rename ' [maindir,subdir(i).name,'\',subsubdir(j).name] ' ' newname]);
num_begin=num_begin+1;
fprintf('当前处理文件夹%s',subdir(i).name);
fprintf('已经处理%d张图片\n',n);
n=n+1;
pause(0.1);%可以将暂停去掉
end
end
end
end
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
后记:Python大法好!
Windows下对文件夹下所有图片批量重命名(附C++,python,matlab代码)的更多相关文章
- Windows操作系统单文件夹下到底能存放多少文件及单文件的最大容量
本文是转自:http://hi.baidu.com/aqgjoypubihoqxr/item/c896921f8c2eaba5feded5f2 最近需要了解Windows中单个文件夹下 ...
- 引用AForge.video.ffmpeg,打开时会报错:找不到指定的模块,需要把发行包第三方文件externals\ffmpeg\bin里的dll文件拷到windows的system32文件夹下。
引用AForge.video.ffmpeg,打开时会报错:找不到指定的模块,需要把发行包第三方文件externals\ffmpeg\bin里的dll文件拷到windows的system32文件夹下. ...
- cocos2d-x3.2下获取文件夹下所有文件名的方法
这里提供一个函数获取文件夹下所有文件名的方法,直接上代码了. 原文地址:http://blog.csdn.net/qqmcy/article/details/36184733 // // Visib ...
- linux_添加定时任务,每5min清理下某个文件夹下的文件
性能测试的过程中会生成大量的日志文件,导致无法继续进行,linux可以增加一个定时任务,进行定时清理 1. 先编写一个sh脚本,该sh脚本用于文件夹文件清理,脚本编写完成后拷贝到服务器上,且授予权限 ...
- Windows下.svn文件夹的最简易删除方法(附linux)
如果想删除Windows下的.svn文件夹,通过手动删除的渠道是最麻烦的,因为每个文件夹下面都存在这样的文件.下面是一个好办法:建立一个文本文件,取名为kill-svn-folders.reg(扩展名 ...
- Java-Maven(九):Maven 项目pom文件引入工程根目录下lib文件夹下的jar包
由于项目一些特殊需求,pom依赖的包可能是非Maven Repository下的包文件,因此无法自己从网上下载.此时,我们团队git上对该jar使用. Maven项目pom引入lib下jar包 在ec ...
- Delphi下遍历文件夹下所有文件的递归算法
{------------------------------------------------------------------------------- 过程名: MakeFileLis ...
- windows下遍历文件夹下的文件
#include <io.h>#include <stdio.h>#include <iostream>using namespace std;int ReadSt ...
- 关于SpringBoot下template文件夹下html页面访问的一些问题
springboot整合了springmvc的拦截功能.拦截了所有的请求.默认放行的资源是:resources/static/ 目录下所有静态资源.(不走controller控制器就能直接访问到资源) ...
随机推荐
- Install and Configure NFS Server on RHEL 8 / CentOS 8
https://computingforgeeks.com/install-and-configure-nfs-server-on-centos-rhel/
- Windows系统批处理命令实现计划关机
操作步骤: 1.新建一个文本文件,粘贴下面代码,保存为shutdown.bat @echo off echo 请输入延迟关机分钟数 echo 小于1分钟将视为立即关机,负数为取消关机 set /p t ...
- Golang的运算符-比较运算符
Golang的运算符-比较运算符 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.比较运算符概述 比较运算符也称为关系运算符,比较运算符返回的类型为bool类型,常见的比较运算符 ...
- CAN网络上新增加的设备与网络上已有设备MAC地址冲突的软件解决方案
已知 1号的CAN节点的地址是0x1f 2号的CAN 节点的地址是0x1f 要达到的要求是 假设 网络上 CAN1 节点已经工作了,我现在需要在网络上接入CAN2节点. 那么CAN2节点首次上电的时候 ...
- js实现二叉查找树
二叉树的特点: 像一颗树一样,从顶端往下延伸,最顶端的为根节点,每个节点下面子节点的数不超过两个,没有任何子节点的节点被称为叶子节点, 除了根节点和叶子节点的被称为中间节点. 二叉查找树: 每个节 ...
- Java并发读书笔记:线程通信之等待通知机制
目录 synchronized 与 volatile 等待/通知机制 等待 通知 面试常问的几个问题 sleep方法和wait方法的区别 关于放弃对象监视器 在并发编程中,保证线程同步,从而实现线程之 ...
- 开源DDD设计模式框架YMNNetCoreFrameWork第6篇-.net Core Logging和Nlog结合
源码地址:https://github.com/topgunymn/YMNNetCoreFrameWork 遇到的坑:使用了Nlog以后,.NETcore自带的日志等级不起作用,只有nlog配置配置文 ...
- 五十八、SAP中常用预定义数据类型
一.SAP中常用预定义数据类型 注意事项如下: 1.默认的定义数据类型是CHAR. 2.取值的时候C型默认从左取,N型从右取,超过定义长度则截断. 3.C类型,可以赋值数值,也可以赋值字符,还可以混合 ...
- 【转】美团 MySQL 数据实时同步到 Hive 的架构与实践
文章转载自公众号 美团技术团队 , 作者 萌萌 背景 在数据仓库建模中,未经任何加工处理的原始业务层数据,我们称之为ODS(Operational Data Store)数据.在互联网企业中,常见的 ...
- [题解] LuoguP3321 [SDOI2015]序列统计
感觉这个题挺妙的...... 考虑最暴力的\(dp\),令\(f[i][j]\)表示生成大小为\(i\)的序列,积为\(j\)的方案数,这样做是\(O(nm)\)的. 转移就是 \[ f[i+1][j ...