reedkm has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I'm very novice, and have tried my hand at this problem:

I have 1000s of PDFs named arbitrarily and non-uniformly by a scanning vendor (nothing at all like the sample here). I need to rename them all with unique and uniform filenames.

I have the paths to all the files, and the metadata needed to uniquely name them. I thought I could create a Perl script that would use an array with the original filepath/name and an array with the new filepath/name to rename.

Thus far I have this:

#!/usr/bin/perl use warnings; use strict; use diagnostics; use File::Copy; # Set up arrays with original name and new name with unique ISBN my @IDI_name = ('I:\Production\TEST\P001.txt', 'I:\Production\TEST\P00 +2.txt'); my @PLL_name = ('I:\Production\TEST\ISBN_Text.txt', 'I:\Production\TES +T\ISBN2_Text.txt'); # Walk through array and copy with new filename foreach my $IDI_name (@IDI_name) { copy("$IDI_name", "$PLL_name"); }

The new name is giving me an explicit package name error, but I can figure out how to declare that variable.

So, is my approach entirely wrong?

How do I declare that variable?

Replies are listed 'Best First'.
Re: Renaming multiple files with unique identifiers
by toolic (Bishop) on Aug 26, 2013 at 20:34 UTC
    No need to declare that variable; just use the arrays. Maybe you're looking for this (untested):
    use warnings; use strict; use diagnostics; use File::Copy; # Set up arrays with original name and new name with unique ISBN my @IDI_name = ('I:\Production\TEST\P001.txt', 'I:\Production\TEST\P00 +2.txt'); my @PLL_name = ('I:\Production\TEST\ISBN_Text.txt', 'I:\Production\TES +T\ISBN2_Text.txt'); # Walk through array and copy with new filename for my $i (0 .. $#IDI_name) { copy($IDI_name[$i], $PLL_name[$i]); }

      Thank you, this does work as well. Much appreciated.

Re: Renaming multiple files with unique identifiers
by choroba (Cardinal) on Aug 26, 2013 at 20:35 UTC
    There is no scalar variable $PLL_name, only the array. Do not use the same name for an array and a scalar, it only confuses humans.

    Possible solution:

    #!/usr/bin/perl use warnings; use strict; use diagnostics; use File::Copy; my @IDI_names = ('I:\Production\TEST\P001.txt', 'I:\Production\TE +ST\P002.txt'); my @PLL_names = ('I:\Production\TEST\ISBN_Text.txt', 'I:\Production\TE +ST\ISBN2_Text.txt'); for my $idx (0 .. $#IDI_names) { my $IDI_name = $IDI_names[$idx]; my $PLL_name = $PLL_names[$idx]; copy($IDI_name, $PLL_name); }

    Perl is not a shell. There is no need to double quote single scalar variables.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      Excellent, this does work. Thank you. And I take your point about naming--it was confusing.

Re: Renaming multiple files with unique identifiers
by hdb (Monsignor) on Aug 27, 2013 at 06:44 UTC

    While your problem has already been solved by the comments above, another approach could be to use a hash as a translation table for your file names. This way you would not need to iterate through parallel arrays but have the mapping between the IDI and PLL name stored in one structure:

    #!/usr/bin/perl use warnings; use strict; use diagnostics; use File::Copy; # Set up hash with original name and new name with unique ISBN my %names = ( 'I:\Production\TEST\P001.txt' => 'I:\Production\TEST\ISBN_Text +.txt', 'I:\Production\TEST\P002.txt' => 'I:\Production\TEST\ISBN2_Tex +t.txt' ); # Walk through hash and copy with new filename while( my( $IDI_name, $PLL_name ) = each %names ) { copy( $IDI_name, $PLL_name ); }