News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

File Renaming Library

Started by Vortex, June 11, 2016, 04:53:28 AM

Previous topic - Next topic

Vortex

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

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

FILE RENAMING MACROS

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

Vortex

New version uploaded at the top.

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

Test environment :

@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 =====
Pause

Vortex

Real life example to rename some complicated filenames :


dir *.cab /b

windows6.1-kb2552343-x64_51acdacad4855a2a04f35e2ac511fd3832a5d931.cab
Windows6.1-KB3020369-x64.cab
windows6.1-kb3078601-x64_785cd5e3b02e0afdac45c6d7c481ba9fd7edbdfc.cab
windows6.1-kb3087039-x64_6543ecf25746dc015c53f26ec9a80d0024f67c9c.cab
windows6.1-kb3109094-x64_570b08f22988118cfa06a014be2a7b66ab493802.cab
Windows6.1-KB3125574-v4-x64.cab
windows6.1-kb3145739-x64_85e13f931690feffe03598bf86ff429d3138dedc.cab
windows6.1-kb3161664-x64_c51e3b68c4df14c7484c3629598c9e96c0576d99.cab


Renamer64.exe *.cab ?Cx?I?I?E
RENAME

D:\64bit\
  windows6.1-kb2552343-x64.cab
  Windows6.1-KB3020369-x64.cab
  windows6.1-kb3078601-x64.cab
  windows6.1-kb3087039-x64.cab
  windows6.1-kb3109094-x64.cab
  Windows6.1-KB3125574-v4-x64.cab
  windows6.1-kb3145739-x64.cab
  windows6.1-kb3161664-x64.cab
  (8 Items)


dir *.cab /b

windows6.1-kb2552343-x64.cab
Windows6.1-KB3020369-x64.cab
windows6.1-kb3078601-x64.cab
windows6.1-kb3087039-x64.cab
windows6.1-kb3109094-x64.cab
Windows6.1-KB3125574-v4-x64.cab
windows6.1-kb3145739-x64.cab
windows6.1-kb3161664-x64.cab