Google Answers Logo
View Question
Q: Perl rename files ( No Answer,   1 Comment )
Subject: Perl rename files
Category: Computers > Programming
Asked by: gerry1234-ga
List Price: $15.00
Posted: 03 Nov 2006 18:05 PST
Expires: 21 Nov 2006 10:26 PST
Question ID: 779921
I have sets of binary files named with the extension .db1 (= ending
with the numeral one).  The first parts of the file names have the
structure: name_A01, name_B01, name_C01, ..., name_L01.  The files
come in sets of 12 (=A-L), and there are 12 sets (01-12).  (The second
set would be name_A02, name_B02, name_C02, ..., name_L02.)  The "name"
part is variable in length and consists of letters, numerals, the
underscore character, the dot character and hyphens.  I would like a
Perl (v.5 or higher) script to rename the files in sequence by
prepending three digit numbers to the front of each existing file
name, from 1 to 144.  The file names would then range from
001_name_A02.db1 to 144_name_L12.  My intention is to have a script I
can place in a folder of target files, run the script, and have the
files renamed.  There will be cases where certain files of the 144
file series will be missing in a target set.  I would like the script
to process all files present and will not need any messages indicating
skipped files.  Should your answer involve 144 repetitive lines, where
the structure is the same but only the obvious parts of the file name
change, you need not type that out, just indicate the form clearly and
I will fill it in.  I will check back for answers in a few days.

Here are 3 sample original file names:

The desired edited names:
There is no answer at this time.

Subject: Re: Perl rename files
From: bozo99-ga on 04 Nov 2006 16:16 PST

opendir(DH, ".") or die("opendir");

foreach $f (@f) {
    next unless (-f $f);
    if ($f =~ /^[\w\.-]+_([A-L])(\d\d).db1$/) {
        $n+= 12 * (($2)-1) ;
        $new=sprintf("%03d_%s", $n, $f);
        rename($f, $new) unless (-e $new);

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  

Google Home - Answers FAQ - Terms of Service - Privacy Policy