Lewis' Blog Tales from the trenches of information technology

14Feb/100

Mass renaming files at the OS/2 command line

Download PDF

An interesting head scratcher to which I have been returning almost monthly for some time is the distinct lack of ability to rename files based upon a mask at the OS/2 command line (in a minute, I'll explain why this is a fairly regular occurrence). Of course, OS/2's cmd.exe is not alone in that regard; DOS' COMMAND.COM, 4DOS, 4OS2, JdeBP's 32-bit CMD, Windows' CMD, and even the *nix shells with which I'm familiar don't naturally lend themselves to this kind of flexibility (okay, I lied: you can do it with Bash, of course; see below).

Why a monthly occurrence?

I don't normally send paper invoices to clients anymore. Not only does it kill more trees than it's worth (and no, I'm not an environmental zealot; we grow new trees every day - I'm just the guy who has to buy the paper and the laser toner), but the postage has over the years become a major factor in this decision. Anyone who has to regularly bill clients for outstanding balances knows from whence I speak; send the same client a dunning notice for six months, and that $150 bill has just been whittled down to $147.36 ($0.44 * 6 = $2.64; $150 - $2.64 = $147.36). This just adds the insult to the injury.

Anyway, I generate pdf''s of most of my invoices and then email them. Because the rest of my life is so disorganized, I really, really try to keep my filesystems in some semblance of order. So, under my \Images directory (someday, I'll post a note about the differences between "folders" and "directories"), I have several subdirectories. The two main ones are NY and VA for the locations of each of our offices. Under each of those, I have invoices broken down by year (for monthly statements sent en masse), as well as by client (for individual clients).

So it came to be that I have a directory called \Images\NY\Clientname-with-hyphens, and under that, a series of pdf's, which were originally named Clientname-with-hyphens_2006-10-07_invoice.pdf, etc. (I avoid spaces in filenames like the plague, in most cases - another post will touch on that at some point.) Somewhere along the line, I decided that it would be easier to simply use an acronym for the client name. For the sake of this discussion, let's say I used "ABC." Without thinking one day, I just went to a prompt and typed:

ren *_2*.pdf ABC_*.pdf

which, of course, makes no sense, as how is CMD supposed to know that I want Clientname-with-hyphens_2006-10-07_invoice.pdf renamed to ABC_2006-10-07_invoice.pdf?

So, for years now (no kidding!), I've had this messed up directory with some files named correctly (since I decided to change the convention) and some which are completely skewed, as in ABC_ntname-with-hyphens_2006-10-07_invoice.pdf. <sigh>

So, today I decided to do something about it.

What's the first thing which comes to mind when thinking about string manipulation? Regex, of course. Now, how do we utilize regular expressions at the command line? With a scripting engine, of course. And while REXX is the de facto built-in scripting engine under OS/2, unfortunately, getting REXX to do string manipulations is almost like teahcing a pig to sing...so...what's just about the best engine to use when you need to do string manipulations at the command line (or pretty much anywhere else, for that matter)? Perl, of course.

True confession: My regex is lousy.

It seems that every time I need to do something with regular expressions, I need to pore over manuals and obscure bits and pieces to find exactly what I need. Note to self: just learn it one day - soon.

So, with a little bit of googling, I happened upon this page, which gave me exactly what I wanted. The only tweak for my setup was to change the location of the Perl binary in the script, from /usr/local/bin/perl to J:/perl5/lib (and yes, those are slashes, not backslashes, as the OS/2 port of Perl reads unix-style paths). (Oh, and this wasn't even necessary considering the manner in which I started the script, anyway; I'm just retentive about such things.)

For me, the actual command once in the directory containing the wacky files was:

perl rename.pl 's/ntname-with-hyphens_//g' <Enter>

Yes, that was it.

No kidding.

The actual rename.pl script which I will quote here for the sake of completeness, giving full credit where credit is due (thanks Dean Mah!), is:


#!/usr/local/bin/perl
#
# Usage: rename perlexpr [files]
($regexp = shift @ARGV) || die "Usage: rename perlexpr [filenames]\n";
if (!@ARGV) {
@ARGV = ;
chomp(@ARGV);
}
foreach $_ (@ARGV) {
$old_name = $_;
eval $regexp;
die $@ if $@;
rename($old_name, $_) unless $old_name eq $_;
}


Yes, that's it. What could be more elegant?

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

No trackbacks yet.