[译]使用Boost Regex和Boost File System重命名文件
By robot-v1.0
本文链接 https://www.kyfws.com/applications/file-rename-using-boost-regex-and-boost-file-syste-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 3 分钟阅读 - 1316 个词 阅读量 0[译]使用Boost Regex和Boost File System重命名文件
原文地址:https://www.codeproject.com/Articles/11592/File-Rename-Using-Boost-Regex-and-Boost-File-Syste
原文作者:Gast128
译文由本站 robot-v1.0 翻译
前言
File rename using boost regex and boost file system
使用boost regex和boost文件系统重命名文件
- 下载演示VS2017-53.7 KB(Download demo VS2017 - 53.7 KB)
- 下载源VS2017-24.9 KB(Download source VS2017 - 24.9 KB)
- 下载演示VS2003-261.4 KB(Download demo VS2003 - 261.4 KB)
- 下载源VS2003-23.6 KB(Download source VS2003 - 23.6 KB)
介绍(Introduction)
作为专业程序员,您经常必须重命名许多文件.虽然有潜力,(As a professional programmer, you often have to rename lots of files. While in potential the ‘) move
的命令(’ command from) cmd
可能会做同样的事情,但最终往往陷入混乱.(could do the same, it often ends up in a mess.)
因此,我使用Boost库的正则表达式和文件系统编写了一个简单的工具,其中扫描了用户提供的目录,并使用来重命名了文件.(Therefore I wrote a simple tool using the Boost libraries regex and filesystem, in which a user supplied directory is scanned and files are renamed by using) regex_replace
.请注意,正则表达式语法功能强大且复杂,因此我增加了首先测试提供的格式字符串的可能性.(. Note that regex syntax is powerful and complex and therefore I added the possibility to test first the supplied format string.)
使用简单(Simple Use)
有关以下内容的完整概述(For a complete overview of) boost::regex_replace
, 请参阅(, please refer to) http://www.boost.org/(http://www.boost.org/) .一个简单的用法是例如MP3压缩器从CD上创建了" 01)Bla.mp3",而我更喜欢像" Artist-Bla.mp3"一样存储它们.将" \ d {2} )“作为正则表达式(不带引号)将指示正则表达式引擎搜索两个数字,后跟一个”)".可以用艺术家的名字代替这场比赛.(. A simple use was e.g. that a MP3 compressor created ‘01) Bla.mp3’ from my CD’s, while I preferred to store them like ‘Artist - Bla.mp3’. Feeding ‘\d{2})’ as regular expression (without quotes) will instruct the regex engine to search for two numbers followed by a ‘)’. This match can be replaced by the artists' name.)
码(Code)
该代码是带有两个额外类的简单MFC对话框:(The code is a simple MFC dialog with two extra classes:)
KFrnDirectoryScanner
-薄包装纸(- Thin wrapper around)boost::filesystem
KFrnMoveFile
-进行实际重命名的功能对象(- Function object which does the actual renaming) 唯一的警告是,您最好先对目录内容进行快照,然后再递归调用刚刚重命名的文件.(The only caveat is that you better first take a snapshot of the content of the directory, before you end up in recursively calling a just renamed file.)
建立(Build)
原始代码是使用Visual Studio 2003构建的.当然,绝对有必要拥有Boost(版本1.33)以及上述库的DLL(请参阅Boost的文档或我的另一篇文章"为Visual Studio构建Boost库". ‘). Visual Studio 2017版本不再需要Boost.(The original code is built with Visual Studio 2003. Of course, it is absolutely necessary that you have Boost (version 1.33) and also the DLLs of the above mentioned libraries (see documentation of Boost or my other article ‘Building boost libraries for Visual Studio’). The Visual Studio 2017 version doens’t need Boost anymore.)
改进空间(Room for Improvement)
可能也有:(Probably it would be nice to also have:)
- 浏览按钮(a browse button)
- 保留其历史的组合框(combobox which keeps its history)
历史(History)
- 2005年9月9日:初始职位(9 September 2005: initial post)
- 2018年2月19日:Visual Studio 2017更新(19 Februari 2018: update for Visual Studio 2017)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C++ VC7.1 Windows Visual-Studio VS.NET2003 Dev 新闻 翻译