Author Topic: File Renaming Library  (Read 1305 times)


  • Moderator
  • Member
  • *****
  • Posts: 1704
File Renaming Library
« on: June 11, 2016, 04:53:28 AM »
As mentioned in the inquiry about EZBuild, it has become a major new project that will be released a bit later this year. As part of that project LD Blake and I have been working on a number of long standing problems with Windows.

In this case it's the ongoing bugaboo about programmatically renaming files.  Lets face it, Microsoft's methods are far from adequate and most of the third party tools are so complicated you need a PHD to operate them.  So, what we came up with was a system of simple macros that can be loaded into a wildcard to rename groups of files.

The algorithm can be used in conjunction with FindFirstFile() and FindNextFile() loops by operating on the cFilename member of the WIN32_FIND_DATA structure, producing command lines like this...

Code: [Select]
REN *.doc *W?N
COPY *.* "\\archive\sales\*P?D ?N"

The marcos are capable of very impressive transformations and many of the "evils" of bad filenames can be fixed quite easily. The attached screen shot shows some of their capabilities.

The attached ZIP contains the Renamer algorithm and it's source code in library format, built with Pelles C version 7. You should feel free to include it into your own projects if you like.

The Demo program will let you safely play with the various macros to see how they work and how best to use them. (It is all on-screen. Nothing is written out.)

This is the macro list and some general instructions...

Code: [Select]

These macros operate on the filename. If path
or drive information is present the result is
undefined and could result in data loss.

A Filename is in 2 parts. A Name and an Extension.
The Extension begins with the last dot in the
filename. The Extension is never edited.

Editing pointers are maintained for the Old name
and New name. Macros operate left to right as
encountered using the current pointers for the
New and Old names.

Transformations operate on the old name in-place
and do not advance the editing pointers.

Editing pointers are advanced as the old
name is copied to the new name by the various
editing macros.

Free text in the wildcard is copied to the new
filename and the New name pointer is updated.

Transformations:  (Old name)
    *A          Remove all whitespace
    *B          Burst Pretty Path
    *C          Convert to ALL CAPITALS
    *D          Spaces to dashes
    *L          Convert to lower case
    *M          Punctuation marks to spaces
    *P          Convert to PrettyPath
    *S          Convert to Sentence case
    *T          Convert to Title Case
    *U          Spaces to underscores
    *W          Remove excess whitespace

Editing: (New name)
    ?A          Insert user account name
    ?Cc         Copy to character
    ?D          Insert today's date as YYYY-MM-DD
    ?E          Copy the extension
    ?F          Copy rest of filename
    ?H          Insert Host computer name
    ?I          Copy next character
    ?J          Jump over next word
    ?L          Copy next character as lower case
    ?N          Copy rest of filename, add extension
    ?O          Omit next character
    ?Sc         Skip to character
    ?T          Insert current time as HH-MM-SS
    ?U          Copy next character as upper case
    ?W          Copy next word
« Last Edit: June 19, 2016, 06:39:14 PM by Vortex »


  • Moderator
  • Member
  • *****
  • Posts: 1704
Re: File Renaming Library
« Reply #1 on: June 19, 2016, 06:41:17 PM »
New version uploaded at the top.

A couple of macros have changed and a new test utility has been added.

Test environment :

Code: [Select]
@echo off
echo Test run for Renamer32.exe
echo =====
if not exist Test1 md Test1
if not exist Test2 md Test2
echo Writing test files
echo "Test File #1 - Marketing"   >"test1\marketing__Projection.txt"
echo "Test File #2 - Production"  >"test1\Production - schedule.txt"
echo "Test File #3 - Sales"       >"test1\sales_ _summary.txt"
echo "Test File #4 - Service"     >"test1\Service_report.txt"
echo "Test File #4 - Tech   "     >"test1\Tech Support.Log.txt"
echo =====
echo test1\
dir /b test1
echo =====
echo renamer32 -co test1\*.* "test2\*M*W*T?Y-?M ?N"
renamer32 -co test1\*.* "test2\*M*W*T?Y-?M ?N"
echo =====
echo test2\
dir /b test2
echo =====


  • Moderator
  • Member
  • *****
  • Posts: 1704
Re: File Renaming Library
« Reply #2 on: June 23, 2016, 04:02:06 AM »
Real life example to rename some complicated filenames :

Code: [Select]
dir *.cab /b

Code: [Select]
Renamer64.exe *.cab ?Cx?I?I?E

  (8 Items)

Code: [Select]
dir *.cab /b