develooper Front page | perl.beginners | Postings from February 2011

Re: Renaming with a specific spec

Thread Previous | Thread Next
From:
Rob Dixon
Date:
February 1, 2011 05:19
Subject:
Re: Renaming with a specific spec
Message ID:
4D4803E4.906@gmx.com
On 01/02/2011 02:41, Harry Putnam wrote:
> Looking for a renaming tool with certain capabilities, but when
> googling or searching cpan its quite hard to tell if the tool can or
> not satisfy them.  So, I hope someone can tell me right off the top of
> their head if there is a renaming tool on cpan or anywhere else for
> that matter that can handle ny specifications:
>
> (This tool will be used against images with fixed extensions)
>
> 1) Handle windows style names with spaces, but not itself create such
>     names.
>
> 2) Generate random names for each file with the least number of
>     characters possible to generate a given number of names.
>     Names can be alph-numeric and should be no longer than necessary to
>     generate the needed number of names (Which will be known in advance)
>
> I've seen quite a few different renaming tools, not necessarily perl
> tools, but they seem to all stick to something of a certain set of
> options for Image naming.  These revolve around various numbering
> schemes, mostly some kind of sequential with varying number of digits,
> insert specific text, and keep extension either upper or lower case.
>
> I have need of generating names that are random, no sequence, but of
> course the extension would remain the same.
>
> So in summary, a renaming tool that can handle windows file naming
> conventions and generate random names (except the extension)
>
> Can anyone suggest a tool they know to be capable of the above?

Hey Harry

As Jim says, there is very little chance that you will find an existing
module that conforms to such a tight specification, but such a facility
is trivial to write. Take a look at the program below and see if it
helps you with a solution.

HTH,

Rob


use strict;
use warnings;

use List::Util qw/shuffle/;

sub digits_for {
   my $n = shift;
   return $n < 36 ? 1 : 1 + digits_for($n/36)
}

sub base36 {
   my ($n, $digits) = @_;
   my $b36 = '';
   while ($digits > 0) {
     my $d = $n % 36;
     $d = chr($d + ord('A') - 10) if $d >= 10;
     $b36 = $d.$b36;
     $n = int $n / 36;
     $digits--;
   }
   return $b36;
}

sub name_list {
   my $num = shift;
   my $digits = digits_for($num);
   my $max = 0;
   $max = $max * 36 + 35 for 1..$digits;
   my @list = map base36($_, $digits), (shuffle 0 .. $max)[0..$num-1];
   return \@list;
}

chdir 'path/to/directory' or die $!;

opendir my $dh, '.' or die $!;
my @names = grep { not /\A\.\.?\z/ and -f } readdir $dh;
closedir $dh;

my $newnames = name_list(scalar @names);

foreach my $name (@names) {
   (my $newname = $name) =~ s/.*(\..*)/pop(@$newnames).$1/e;
   printf "%s => %s\n", $name, $newname;
   rename $name, $newname;
}

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About