Mass renaming files at the OS/2 command line
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?
Last Updated on by LewisR
Related posts:
- A sincere apology to users of my YUM repo mirror No good deed goes unpunished. Setting things in motion...
- The Telltale Hard Drive Sometimes, the whole power management and power saving business can...
- CRTs vs LCDs in 2011/2012 Is all new technology better technology? Why upgrade what isn't...
- Configuring Squid Proxy on OS/2: Path adjustments Who says you can't teach an old dog new tricks?...
- Enabling Extended Attribute support in a Thecus N2200-EVO It's refreshing that some manufacturers actually do provide a degree...
Enjoy this article?
Recent Posts
- Novell Client for Windows (32-bit) Internal Error 0x00008993
- Noisy utility company email
- The importance of Common User Access design guidelines in 2018
- Navigating Coinbase’s customer support
- Configuring the IOGEAR GWU627 wireless ethernet bridge device under ArcaOS (and OS/2)
Categories
Support Pages
Posts by Date
S | M | T | W | T | F | S |
---|---|---|---|---|---|---|
« Jun | ||||||
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Log In
Email Notifications
RSS Feeds
Recent Comments
- LewisR on Installing Windows Server 2008 R2 x64 on the HP Proliant DL380 G4
- LewisR on Installing Windows Server 2008 R2 x64 on the HP Proliant DL380 G4
- justintd on Installing Windows Server 2008 R2 x64 on the HP Proliant DL380 G4
- LewisR on WP Post to PDF Enhanced
- pdfsc on WP Post to PDF Enhanced
Leave a comment
You must be logged in to post a comment.