Re: Mass file renaming
by BrowserUk (Patriarch) on Jul 19, 2005 at 20:11 UTC
|
print for map {
$_ - 9900
} 22200010001, 22200010101, 22200010102, 22200010103, 22200010201;;
22200000101
22200000201
22200000202
22200000203
22200000301
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
| [reply] [d/l] |
|
|
@old_names = (
22200010001, 22200010101, 22200010102, 22200010103, 22200010201
);
foreach (@old_names) {
my $old_name = $_;
my $new_name = $_ - 9900;
... do something ...
}
| [reply] [d/l] [select] |
Re: Mass file renaming
by Zaxo (Archbishop) on Jul 19, 2005 at 20:08 UTC
|
You need to specify exactly what transformation you want to make on the file names. Write that as a sub,
sub name_transform {
my ($oldname, $newname) = shift;
$newname = ;# whatever
return $newname;
}
Then rename your files with something like this,
for (glob '/path/to/222*') {
rename $_, name_transform($_);
}
| [reply] [d/l] [select] |
Re: Mass file renaming
by CountZero (Bishop) on Jul 19, 2005 at 20:03 UTC
|
If you can explain to us the general pattern of the filenames to be changed and the rule used to change them into another file name, I'm sure someone will be able to write a nice regex for it.
CountZero "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law
| [reply] |
Re: Mass file renaming
by sasikumar (Monk) on Jul 19, 2005 at 21:16 UTC
|
Hi,
Might be this would work.
sub TransformFileName($)
{
my $FileName=shift;
my $IdExp1=$FileName;
my $IdExp2=$FileName;
my $IdExp3=$FileName;
$IdExp1=~s/(\d\d\d)(.*)?/$1/;
$IdExp2=~s/(\d\d\d)(\d\d\d\d\d\d)(.*)?/$2/;
$IdExp3=~s/(\d\d\d)(\d\d\d\d\d\d)(\d\d)/$3/;
my @Transform=split(//,$IdExp2);
my $value=0;
for(my $i=0;$i<@Transform;$i++)
{
$value+=$Transform[$i];
}
my @Nsize=split(//,$value);
my $Nzero=5;
$IdExp2=$value;
for (my $i=0;$i<$Nzero-@Nsize;$++)
{
$IdExp2='0'.$IdExp2;
}
my $TransformedFileName=$IdExp1.$IdExp2.$idExp3;
return $TransformedFileName;
}
I have not tested it. I hope this is what you need.
| [reply] [d/l] |
Re: Mass file renaming
by GrandFather (Saint) on Jul 19, 2005 at 22:06 UTC
|
Seems you want to break the names into three chunks with the prefix and suffix retaining their original values and the middle chunk incrementing every time a new suffix sequence starts.
That being the case something like this should work:
use strict;
use warnings;
my $prefix;
my $midCount;
my $lastSuffix;
while (<DATA>)
{
chomp;
my $oldName = $_;
$prefix = substr $_, 0, 3 if ! defined $prefix;
my $suffix = substr $_, -2;
++$midCount if ! defined ($lastSuffix) or $suffix <= $lastSuffix;
$lastSuffix = $suffix;
my $newName = sprintf ("%03s%06d%02d", $prefix, $midCount, $suffix);
print "$oldName -> $newName\n"
}
Perl is Huffman encoded by design.
| [reply] [d/l] [select] |
Re: Mass file renaming
by rruiz (Monk) on Jul 20, 2005 at 07:52 UTC
|
$filename = 'aa22200010001.png';
You can use either:
$filename =~ s/(\d{3})(\d{6})(\d{2})/sprintf("%03d%06d%02d", $1,$2+1,$
+3)/e;
Or (as pointed by BrowserUK):
$filename =~ s/(\d{11})/$1 + 100/e;
Just note that if your file name var contains the path to the file, and one of the directories in the path contains a similar name (ie. eleven consecutive digits, as in: '/tmp/0123456789019/22200010001.png'), you will have to modify the regexp to take it into account and the replacement part will/may not be so simple.
| [reply] [d/l] [select] |
Re: Mass file renaming
by anonymized user 468275 (Curate) on Jul 20, 2005 at 13:04 UTC
|
...all of which boils down to: perl -e 'map( rename ($_, $_-9900), glob "222*"));'
| [reply] [d/l] |
|
|
perl -e 'rename ($_, $_-9900) for glob "222*";'
--
< http://www.dave.org.uk>
"The first rule of Perl club is you do not talk about
Perl club." -- Chip Salzenberg
| [reply] [d/l] |
|
|
I quite agree that your version with for reads better than map. However, having followed the linked discussion as you suggest, I see no justification for going as far as calling it 'bad' to use map in void context and consider that part an overreaction.
| [reply] |
|
|