Bill Akins wrote:

> Hi all,
>
> After processing files in my perl script, I need to move them to the final
> destination.  My problem is if the file already exists (lets call it
> data.txt), I need to rename it to data-v01.txt (where -v01 is version 01).
>
> So... if there are four other esisting versions, data-v03.txt should become
> data-v04.txt, then data-v02.txt should become data-v03.txt, then
> data-v01.txt should become data-v02.txt, data.txt should become
> data-v01.txt, finally data.txt can be written.
>
> I have to keep all versions of the existing files and oldest must have the
> highest -v## (version number) and newest is just data.txt.  I will end up
> with 20 or more versions of each file.
>
> Is this a job better done in bash or perl?  Either way, any pointers would
> be greatly appreciated!
>
> Thanx!

#!/usr/bin/perl -w
use strict;

my $dir = #Assign your final destination dir to this var

map  {rename ($_->[0], "$dir/" . 'data-v' . sprintf ("%02d", ++$_->[1]) . '.txt')}
      sort { $b->[1] <=> $a->[1] }
      map  { [$_, m/data-v(\d+).txt/] } <$dir/data-v[0-9][0-9]*.txt>;

#The above three lines works like this
# Step 1 : Creates a list of array references. Each reference is an array of the form
# ["$dir/data-v01.txt", '01'] ["$dir/data-v02.txt", '02'] ...

# Step 2 : Sort the list on the array reference index 1, i.e. on '01', '02' ... etc
# in a descending numeric order. This is done to make sure that data-v02.txt
# is renamed before data-v01.txt is renamed to data-v02.txt.

# Step 3 : Rename the files

rename ("$dir/data.txt", "$dir/data-v01.txt");
rename ("data.txt", "$dir/data.txt");

Note :  Please do make sure if this does what you want before you use it. I tested
              this on my m/c based on your mail. But still!!

HTH,
Sudarsan



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to