ReplaceRegexTable of ContentsCopyRegex
RenameRegex

Rename Files Using Regular Expressions

RenameRegex command line program renames files whose name matches a regular expression places the output files in a separate directory if such directory is specified.

Command line parameters are:

-srcdir   adirectory     directory of the original files. By default, this is the current directory.

-destdir   adirectory    destination directory to move the renamed files. By default, this is the source directory. The source files will be renamed if a different destination directory is not specified. If a destination directory is specified, then the files will be moved there.

-renameto   atext     New file name. Captured regular expression groups can be used as $1, $2 and so on. For example: $1.txt

-renamefrom   apattern       the file name pattern to find to rename. For example: (report)_[0-9]*.txt

-casesens presense of this flag means search is case sensitive.

-quotes   this flag means the following characters are used instead of double quotes in the -find and -replace parameters. This is to help escaping quotes inside your find replace parameters.

-r         recursively process sub directories.

RenameRegex.exe is part of Bestcode File Utilities tool set.

Case Study - Simplifying File Names via File Rename using Regular Expressions

An example of using Renameregex tool is when we have many files whose names contain dates, or uniqe random IDs that we wish to strip away and only keep the base file name.

We ran into this problem while developing a data feed solution for our most popular art and photography posters website viewposters.com.

Viewposters.com contains about 300 thousand posters. We retrieve the data in delimited text files to import into our database. To automate this process we have our scripts. These scripts expect simple and clear file names that hold the data. However, here are the cryptic file names we have to start with:

ViewPosters Art and Photography Posters Data Feed

Notice the unique ID that each file name contains.

Here RenameRegex.exe comes to our rescue and enables us to rename all files at once with a simple command:

C:\>RenameRegex.exe -srcdir c:\viewposters -renamefrom "(.*)_.*.Txt" -renameto "$1.Txt"

Rename files using regular expressions

And the output is:

Viewposters data feed files renamed using regsular expressions

We are now ready to import our data feed to display our beautiful posters :-)

RenameRegex.exe is part of Bestcode File Utilities tool set. Get it now and enjoy life.

 

webmaster@gobestcode.com