Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / C++

File Rename Using Boost Regex and Boost File System

Rate me:
Please Sign up or sign in to vote.
2.33/5 (3 votes)
9 Sep 2005CPOL2 min read 51.3K   1.2K   26   4
File rename using boost regex and boost file system

Sample Image - File_rename_using_regex.jpg

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.

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/. 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

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

The original code is built with Visual Studio 2003. It was 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 doesn't need Boost anymore since  C++ incorporates regular expressions and filesystem now (C++17).

Room for Improvement

Probably it would be nice to also have:

  • a browse button
  • combobox which keeps its history

PowerToys

It seems that Micrsoft has added a file renaming tool with regular epxression option to PowerToys. That makes this tool and article pretty much obsolete.

History

  • 9 September 2005: initial post
  • 19 Februari 2018: update for Visual Studio 2017

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Netherlands Netherlands
Born in the Netherlands in 1971. Spent since 1998 years as a software engineer programming Windows application(s) using, UML, C++, MFC, STL, DirectShow and Boost.

https://gast128.blogspot.com/

Comments and Discussions

 
QuestionGreat tool but ask how to do specific job Pin
Asholino9-Jan-19 6:18
Asholino9-Jan-19 6:18 
GeneralLinks Pin
YoSilver10-Sep-05 10:02
YoSilver10-Sep-05 10:02 
GeneralRe: Links Pin
Gast12810-Sep-05 11:17
Gast12810-Sep-05 11:17 
GeneralRe: Links 2 Pin
Gast12811-Sep-05 0:25
Gast12811-Sep-05 0:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.