windos下批量修改文件名(C++学习实践)

Shell扩展编程一:添加windos右键菜单---win10 64位+VS2008 参考资料: 《Windows-Shell扩展编程完全指南》 《如何编程实现扩展右键菜单之一:增加自己的菜单项》https://blog.youkuaiyun.com/chen_wei_d/article/details/5462378 《Windows Shell编程实现右键菜单-VS2005,Win7 32位》https://blog.youkuaiyun.com/zaishaoyi/article/details/38475553 配置环境:win10+vs2008 实现效果: ... 阅读详情

这是改编第N版了,希望有看过用过的,建议一下。vc2010编译的,mingw 4.6.x 静态编译的文件太大了,(微软的原因,大家都知道的。csdn上没有找到上传源文件的地方,只好复制粘贴了。

程序基本选项:
("help,?", "输出帮助")
("verbose,V", "显示程序执行情况")
("imitate,I", "并不真的执行文件名修改,只是模拟显示")
("directory,D", po::value < vector < string > >(), "输入目录名")
("filter,F",po::value < string > (), "筛选特定文件名的文件")
("prefix,P", po::value < string > (), "修改文件名的前缀")
("rule,R", po::value < string > (),"简单修改法则:s/匹配目标/替换/位置")

//
有时候真的觉得,编写一个函数对象去适应STL算法真的很麻烦,还不知道效率如何,最后用添加一个临时容器,一个交换解决swap。
比如:

typedef std::vector<std::string> Container;
Container fileList;

if (vm.count("filter") ){
string filter=vm["filter"].as< string > ();
regex re(filter);
Container v;
BOOST_FOREACH( string fname,fileList){
if( regex_search(fname,re) ) v.push_back(fname);
}
fileList.swap(v);
//remove_if(fileList.begin(),fileList.end(),std::bind(std::regex_search,_1,re) );
}

//
bren.cpp
#include "boost/program_options.hpp"
#include "boost/filesystem.hpp"
#include "boost/progress.hpp"
#include "boost/foreach.hpp"
#include "boost/lexical_cast.hpp"
#include "boost/thread.hpp"
// 
#include <iostream>
#include <functional>
#include <regex>
#include <vector>
#include <string>
// 
using namespace std;
namespace po = boost::program_options;
namespace fs = boost::filesystem;
//
static bool isDisplay = false;
static bool isImitate = false;
static bool noPattern = false;
//
typedef
vector < string > Container;
void pathList(const string& path, Container & fileList);
// 
struct SedFormat {
string pattern_;
string replacement_;
string flag_;
};
bool prase(const string& s, SedFormat & x);
// 
struct Table {
string oldName_;
string newName_;
};
void rule(const string& oldName, const SedFormat & x, Table & y);
void threadRename(const Table & table);
//
struct Record {
long location_;
unsigned long length_;
};
//
typedef vector < Record > Cell;
void record(const string & s, const string & pattern, Cell & t);
//
bool cmp(const Record& a, const Record& b);
void noRecord(const string& s, Cell& t);
// 
int main(int argc, char *argv[])
{
//cout<<__LINE__<<":"<<__FUNCTION__<<"\n";
try{
boost::progress_timer t(clog);
namespace po = boost::program_options;
//int opt;
po::options_description desc("Allowed options");
desc.add_options()
("help,?", "输出帮助")
("verbose,V", "显示程序执行情况")
("imitate,I", "并不真的执行文件名修改,只是模拟显示")
("directory,D", po::value < vector < string > >(), "输入目录名")
("filter,F",po::value < string > (), "筛选特定文件名的文件")
("prefix,P", po::value < string > (), "修改文件名的前缀")
("rule,R", po::value < string > (),
"简单修改法则:s/匹配目标/替换/位置")
;
//
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(desc).run(), vm);
po::notify(vm);
//
if (vm.count("help")) {
cout // <<"这是一个批量改名程序,命令格式如下:\n
// bren -Dir d:\picture -prefix 美女\n"
// <<"结果:d:\backup\美女1.jpg \n d:\backup\美女2.jpg
// ......\n"
<< "Usage: options_description [options]\n" << desc
<<"简单修改法则:s/模式/替换/位置\n"
<<"反模式替换(替换不匹配的模式):!s/模式/替换/位置[必须是数字]\n"
;
return EXIT_SUCCESS;
}
//
if (vm.count("verbose")) {
isDisplay = true;
}
//
if (vm.count("imitate")) {
isImitate = true;
isDisplay = true;
}
// 目录列表
Container fileList;
// 
if (vm.count("directory")) {
const Container& dirs = vm["directory"].as < Container > ();
if( dirs.empty() == true ) return EXIT_FAILURE;
BOOST_FOREACH(string fname, dirs) {
pathList(fname, fileList);
}
if( dirs.empty() == true ) return EXIT_SUCCESS;
}
//
if (vm.count("filter") ){
string filter=vm["filter"].as< string > ();
regex re(filter);
Container v;
BOOST_FOREACH( string fname,fileList){
if( regex_search(fname,re) ) v.push_back(fname);
}
fileList.swap(v);
//remove_if(fileList.begin(),fileList.end(),std::bind(std::regex_search,_1,re) );
}
//
if (vm.count("prefix")) {
string prefix = vm["prefix"].as < string > ();
unsigned long number = 0;
string temp = boost::lexical_cast < string > (fileList.size());
const unsigned int FormatLength = temp.length();
string ss(FormatLength, '0');
Table table;
BOOST_FOREACH(string fileName, fileList) {
string s = boost::lexical_cast < string > (number);
if (s.length() <= FormatLength) {
ss.replace(FormatLength - s.length(), s.length(), s);
// cout<<s<<"\t"<<ss<<"\n";
}
table.oldName_ = fileName;
using namespace fs;
fs::path dir(fileName);
fs::path extension = dir.extension();
table.newName_ = prefix + ss + extension.string();
if (is_regular_file(dir)) {
if (table.oldName_ != table.newName_) {
boost::thread t(threadRename, table);
t.join();
}
}
number++;
}
return EXIT_SUCCESS;
}
// 
if (vm.count("rule")) {
// using namespace boost;
string s = vm["rule"].as < string > ();
string sed("!?s/.*/.*/(-?[0-9]*|[aig])");
regex re(sed);
if (!regex_match(s, re)) {
clog << "错误的修改法则!\n" <<
"简单修改法则:s/模式/替换/位置\n"
"反模式替换(替换不匹配的模式):!s/模式/替换/位置[必须是数字]\n";
return EXIT_FAILURE;
}
SedFormat r;
if (prase(s, r) == false) {
cout << __LINE__ << ":" << __FUNCTION__ << "\n";
return EXIT_FAILURE;
}
regex e(r.pattern_);
Table table;
BOOST_FOREACH(string fileName, fileList) {
rule(fileName, r, table);
if (table.oldName_ != table.newName_) {
boost::thread t(threadRename, table);
t.join();
}
}
}
//
}
catch( const std::exception& e){
cerr<<e.what()<<"\n";
}
return EXIT_SUCCESS;
}


// 
void pathList(const string& path, Container & result)
{
// cout<<__LINE__<<":"<<__FUNCTION__<<"\n";
using namespace fs;
try {
Container dir_temp;
fs::path p(path);
if (!fs::exists(p)) {
clog << "\nNot found: " << p << std::endl;
return;
}
if (p.empty()) {
return;
}
if (fs::is_directory(p)) {
fs::directory_iterator end_iter;
fs::directory_iterator itr(p);
while (itr != end_iter) {
if (fs::is_directory(itr->status())) {
// cout<<"dir:\t"<<itr->path().file_string()<<"\n";
dir_temp.push_back(itr->path().string());
}
result.push_back(itr->path().string());
itr++;
}
}
}
catch(const filesystem_error & e) {
clog << e.what() << "\n";
}


}


//
bool prase(const string& s, SedFormat & x)
{
//cout << __LINE__ << ":" << __FUNCTION__ << "\n";
if( s[0]=='!' ) noPattern=true;
const char FLAG = '/';
auto itr1 = find(s.begin(), s.end(), FLAG);
auto itr2 = find(itr1 + 1, s.end(), FLAG);
auto itr3 = find(itr2 + 1, s.end(), FLAG);
string pattern(++itr1, itr2);
string replaced(++itr2, itr3);
string flag(++itr3, s.end());
x.pattern_ = pattern;
x.replacement_ = replaced;
x.flag_ = flag;
//
return true;
}


//
void record(const string & s, const string & pattern, Cell & t)
{
auto itr = s.begin();
auto end = s.end();
smatch m;
regex re(pattern);
Record record;
while (regex_search(itr, end, m, re)) {
if (m.empty())
break;
record.location_ = m[0].first - s.begin();
record.length_ = m[0].second - m[0].first;
t.push_back(record);
itr = m[0].second;
//cout<<s<<"\t"<<record.location_<<"\t"<<record.length_<<"\t"<<m[0]<<"\n";
}
}
//
bool cmp(const Record& a, const Record& b){
return ( a.location_ < b.location_ );
}
//
void noRecord(const string& s,Cell& t){
//clog<<__LINE__<<":"<<__FUNCTION__<<"\n";
if( t.empty() == true ){
return;
}
Cell x(2);
auto head=x.begin();
auto end= x.end();
--end;//指向最后一个元素
auto itr=t.begin();
auto endItr= t.end();
--endItr;//指向最后一个元素
unsigned long last = endItr->location_ + endItr->length_ ;
//
if( last == s.length() ){
x.erase(end);
end=head;
}
else{ 
end->location_ = last;
end->length_ = s.length() -last;
}
//
if( itr->location_ == 0 ){
x.erase(head);
}
else
{
head->location_ = 0;
head->length_ = itr->location_;
}
//
Record a;
while( itr != endItr){
a.location_ = itr->location_ + itr->length_ ;
++itr;
a.length_ = itr->location_-a.location_;
if(a.length_ == 0 ) continue;
x.push_back(a);
}
sort( x.begin(),x.end(),cmp );
t.swap(x);
}
//
void rule(const string& oldName, const SedFormat & x, Table & y)
{
//cout << __LINE__ << ":" << __FUNCTION__ << "\n";
y.oldName_ = oldName;
y.newName_ = oldName;
//
if (x.pattern_.empty()) return;
//
regex re(x.pattern_);
//
if (x.flag_.empty()) {
y.newName_ = regex_replace(oldName, re, x.replacement_,
regex_constants::format_first_only);
return;
}
//
string fm = "$&";
regex rf("[aig]");
if (regex_match(x.flag_, rf)) {
if ("a" == x.flag_)
fm += x.replacement_;
if ("i" == x.flag_)
fm = x.replacement_ + fm;
if ("g" == x.flag_)
fm = x.replacement_;
y.newName_ = regex_replace(oldName, re, fm);
// cout<<fm<<"\t"<<oldName<<"\t->"<<newName<<"\n";
return;
}
//
regex rn("-?[0-9]*");
if (regex_match(x.flag_, rn)) {
Cell table;
record(oldName, x.pattern_, table);
if( noPattern == true ){
noRecord(oldName,table);
}
if (table.empty())
return;
long loc = boost::lexical_cast < long >(x.flag_);
//cout<<__LINE__<<":\t"<<loc<<"\t"<<table.size()<<'\n';
if (labs(loc) > table.size() ) {
return;
}
if (loc > 0) {
loc--;
}
else if (loc < 0) {
loc += table.size();
}
y.newName_.replace(table[loc].location_,table[loc].length_, x.replacement_);
return;
}
//
}


//
void threadRename(const Table & table)
{
using namespace fs;
try {
if (isImitate == false)
rename(table.oldName_, table.newName_);
if (isDisplay == true)
cout << table.oldName_ << "\t->\t" << table.newName_ << "\n";
}
catch(const filesystem_error & e) {
cerr << e.what() << "\n";
}
}


//

c++批量重命名文件 下的批量重命名文件工具都不好用,所以做了一个简单的批量重命名文件,专门对电视剧和动画刻录使用 立即下载

相关推荐

codemirror6 制作markdown编辑器(源码模式)

总体上感觉能用,但是不怎么友好,官方文档有点散乱,而且关键字高亮功能是独立出去的,去官方文档找半天,也没找到怎么改。EditorView.theme可以往EditorView模块中修改CSS的样式,从而影响编辑器的样式展示。"### 测试\n\n```javascript\nlet x = 'y'\n```"这个插件中的markdown语法的高亮样式。//这里指定markdown中代码块使用的解析扩展。开头的class样式,并且还需要在选择器前面添加。//markdown语言解析扩展。

qq_43203949的博客 1784

c++ 命名规则 private_1分钟重命名10000个文件,超简单终极版0难度,不接受反驳...

工作中,多多少少会遇到文件重命名的问题,鼠标单击右键重命名大家都懂,但是如果有五个或以上的文件需要重命名,这样改法不知道改到猴年马月才能改完。今天依依就来跟各位介绍三快速批量文件重命名的方法,小本本记起来哟~相比大家上网找图保存的时候都是默认文件名,一是命名麻烦二是懒,等到要用的时候找起来就麻烦了,但十几甚至上百张的照片一个个重命名可不是个轻松事,所以这必杀技必须GET起来~一、快捷键法所谓的快捷...

weixin_39668898的博客 270

双足步行机器人的ZMP与CoP检测

静态步行与动态步行   机器人步态分为静态步行和动态步行。当机器人做静态步行运动时,身体的各个部分运动速度很小,机器人的整体稳定性较易控制。静态步行稳定性采用机器人的重心地面投影点(Center of Gravity,简称CoG)作为稳定性标准,这种判定方法适用于运动较为缓慢的情况。如果机器人采用的是一种高速运动的步行方式,则称之为动态步行。当机器人进行动态步行时,其重心的位置和加速度的大小时...

weixin_34112208的博客 5909

c++实现文件的批量重命名

c++实现文章的批量重命名

hesphoros的个人博客 1074

Ansys Lumerical 启动时出现 Error: Could not connect to Ansys license server specified at 1055@ 的另一种解决办法

昨天在关闭了中之后 就能正常打开 FDTD 了,结果今天想开启出现了同样的报错:我猜测昨天能启动的原因在于关闭之后要重启电脑,并且在重启之后我第一时间打开 lumerical 测试因此端口没有被占用,今天是在电脑启动之后一段时间才打开所以又遇到了端口占用的问题。

qq_51404184的博客 9554

FPGA开发者必看:手把手教你用Xilinx ERNIC IP实现RoCE v2网络加速(附PG332文档解读)

本文详细解析了Xilinx ERNIC IP在FPGA平台上实现RoCE v2网络加速的全流程,包括架构设计、Vivado工程配置、驱动开发与性能调优。通过PG332技术文档的实践指导,帮助开发者快速构建低延迟、高性能的网络加速方案,适用于数据中心和边缘计算场景。

weixin_29168813的博客 670

C++之文件批量改名

批量文件改名

事业不等于爱好 1798

Triton推理服务器实战:如何为PaddleOCR的ONNX模型配置动态批处理和并发推理

本文详细介绍了如何使用Triton推理服务器为PaddleOCR的ONNX模型配置动态批处理和并发推理,以提升GPU利用率和推理性能。通过实战案例和参数调优指南,帮助开发者解决高并发场景下的性能瓶颈问题,实现从实验室到生产环境的高效部署。

weixin_42527160的博客 426

用c++实现文件批量操作

Hello,大家好,我是文宇。在C++中实现对文件的批量操作(例如批量重命名、移动、删除等),一般会涉及到目录遍历和文件操作的相关函数。可以使用C++标准库中的库(在C++17及以后的版本中可用)来实现这一功能。

文宇的博客 774

RoCEv2 (RDMA over Converged Ethernet) 技术详细介绍

本文深入解析RDMA over Converged Ethernet (RoCEv2)技术,该技术是AI时代算力网络的核心。文章从四个维度展开:首先阐述RDMA如何通过零拷贝和内核旁路解决传统网络的数据搬运瓶颈;接着拆解RoCEv2协议栈,对比其与InfiniBand和iWARP的特性差异;然后分析其在分布式AI训练、高性能存储等场景的关键价值;最后提供实战指南,强调无损网络配置、流量隔离和精细监控的重要性。RoCEv2虽能带来微秒级延迟和极低CPU开销,但需精心设计网络环境才能发挥最大效能。

技术引领业务创新 1392

MATLAB - 浮动基座机器人的逆运动学

本例演示如何解决以浮动底座为模型的机器人的逆运动学问题。浮动底座机器人可以在空间中自由平移和旋转,具有六个自由度。浮动基座机器人的逆运动学问题适用于空间应用,即使用安装在浮动和致动基座上的机械臂在空间操纵物体,也适用于躯干和腿部可在空间自由移动的行走机器人。请注意,刚体树对象表示具有固定基体的刚体树。这意味着刚体树的基体在世界中具有固定的位置和方向。您可以在基体上附加一个 “浮动 ”类型的刚体关节,为具有浮动基体的机器人建模,以用于动态模拟等某些应用。

机器人控制与仿真应用 2440

C++ Windos API FindNextFile函数

上文介绍了FindFirstFile函数,本文介绍与FindFirstFile搭配使用的FindNextFile函数,两者配合使用能够遍历与指定文件名模式匹配的所有文件或目录;

m0_64240990的博客 1411

VTK:vtkAssembly用法实战

VTK:vtkAssembly用法实战程序输出程序完整源代码 程序输出 程序完整源代码 #include <vtkActor.h> #include <vtkAssembly.h> #include <vtkAxesActor.h> #include <vtkCubeSource.h> #include <vtkNamedColors.h> #include <vtkNew.h> #include <vtkOrientation

希望我的博客,能帮上你解决学习中工作中所遇到的问题 936

C++入门

C++入门知识

little_startoo的博客 144

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

【工具-Visual Studio Code】

第一步:(点击设置)

sinat_23896491的博客 3088

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

Windows快速使用vscode调试C

修改后在当前文件夹下自动生成.vscode文件夹,文件夹里面都是配置文件,.vscode下同时生成c_cpp_properties.json文件,是C/C++属性配置文件。快捷键,或菜单栏help->show all commands在弹出的界面中选择:[C/C++:Edit Configurations(UI)],然后进入设置页面。(3)终端---配置任务,菜单栏Terminal->Configure Tasks,生成任务配置文件tasks.json。添加成功后在终端输入gcc -v 会显示gcc信息。

Simona_gy的博客 900

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

Windows与Linux换行符差别

这问题坑了我一天 话不多说,直接上命令!!!! 以后在window用代码批量生成文件的时候,一定要多注意!!!容易出现这种问题!!! 查看文本编码格式用 cat -A 修改文本格式用 dos2unix 文件名 window用代码生成的文件在linux打开 用dos2unix转化后 参考文献 Windows与Linux换行符差别所引起的问题的解决方法 ...

Piepis的博客 251

linux 文本编辑器:vi、vim、实用技巧、vimplus、插件管理器vundle、Nano编辑器

Vim / Vi 是一个功能强大的全屏幕文本编辑器。vim ( Vi IMproved ) 是 vi的一个加强版,主要用来 "创建、编辑、显示" 文本文件。现在主流的 linux 都会安装 vim 文本编辑器。Vim/Vi 没有菜单,只有命令。linux 下还有另外2个强大的编辑器 Emacs、Nano。

freeking101的博客 4万+

linux命令详解及软件安装(持续更新)

linux解析 在java开发之路(javase、Mysql、前端(html、Css、Js)、JavaWeb、SSM(到这里很多人就开始找工作了)、Springboot、Vue、SpringCloud、MybatisPlus、Git…) 走到一定程度之后。 接下来再要遇到的技术(消息队列(kafka、RabbitMQ、RockeetMQ)、缓存(Redis)、搜索引擎(ES)、集群分布式(需要购买.....................

Ferao的博客 1万+

Node.js环境搭建:nvm多版本管理实战指南

Node.js环境配置是前端与全栈开发的基石,其本质涉及操作系统兼容性、Shell路径管理、权限模型与原生模块编译链路等底层原理。掌握nvm(Node Version Manager)这一跨平台版本管理工具,可有效规避官方安装包导致的PATH污染、权限风险、架构错配(如M1 Mac的arm64/x64混淆)及npm版本绑定问题。技术价值在于实现用户级沙盒隔离、秒级版本切换与企业级审计能力,广泛应用于Vue/React项目初始化、CI/CD流水线构建、Linux服务器生产部署及混合技术栈(如Node.js+J

wjxbj的博客 666

基于开源飞控pix的无人机装调与测试

适用人群:希望进一步掌握无人机装调的飞手从零搭建无人机硬件平台的学生/教师无人机爱好者,开发者其他尚未入门开源PX4固件调试的学习者直播回放无人机装调与测试(一):无人机基本常识/预备知识固件是是烧录在飞控硬件中的软件,pixhawk飞控支持PX4和ArduPilot两种版本的固件。本次课程重点以PX4固件为基础进行讲解PX4 1.10以上固件QGC固件版本1.11一、将要观察的数据声明成uORB消息,并发布。我这里随便添加了一个。

超维空间科技的博客 2万+

c++如何批量修改文件后缀名

1、问题描述 在日常的应用中,很多时候会用到批量修改图片的后缀名。比如将1.bmp修改为1.jpg。或许我们用格式工厂或者批处理文件.bat课以轻而易举的做到。这里暂不讨论。本篇文章仅仅对如何用c++做图片批处理修改后缀名做一些说明。 2、c++程序 程序思路:先获取到文件夹下所有文件的名字,然后,用newname取代原来的oldname。#include "stdafx.h"#include

阅读修身 4672

【c++】12每日一练,批量替换字符串/行编辑程序/用栈实现字符串的替换,海贼oj174

适合c++新手的日常练习

2301_80705795的博客 423

c 语言 批量更改文件名,rename 批量修改文件名

rename与mv的区别就是mv只能对单个文件重命名,而rename可以批量修改文件名linux中的rename有两种版本,一种是C语言版的,一种是Perl版的,如何区分系统里的rename是哪个版本的,可以通过一下方式:输入man rename 看到第一行中包含Linux Programmer’s Manual时,这就是C语言版本的,如果第一行中包含Perl Programmers Refer...

weixin_42277672的博客 2148

C++替换文件中指定的内容

/*实现替换文件中指定的内容 ----Created by cryking---- --------2012.02.12--------*/ #include&lt;iostream&gt; #include&lt;fstream&gt; #include&lt;string.h&gt; #include&lt;stdlib.h&gt; using namespace std; char *st...

数据库 2154

VC实现实用文件按指定名字批量改名

前几天有哥们工作需要用到按指定文件名批量改名,在网上找了一下一般都不好用,也难的去找。自己用vc实现了一个简单程序完成这个任务,实现还是很简单,大致流程就是把图片路径读取,然在导入批量的文件名或者手动输入也可然后进行改名。

clive_hua的博客 1089
上一篇: 土豆网视频文件在哪里?
wukexin
博客等级 码龄27年 3粉丝 6原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值