Using an input/csv file to rename files

T

Terry

I am a few hours into my life after Perl.

I need to rename approximately 5000 .jpg files, which I think I have
figured out thanks to "Learning Perl" 3rd. Ed. (pg. 176).

Unlike that example, I would like to compare/match the file names to a
table/field in a csv file and once matched rename the file to the
second table/field in the csv file so that ABCDEFG.jpg becomes
1111111.jpg and ABCDEFH.jpg becomes 1111112.jpg and so on.

I've been searching my perldoc, comp.lang.perl.misc and "Learning Perl"
most of the day and haven't found anything that looks like what I want
to do, but I'm chalking that up to not knowing where to look.

Any suggestions of howto: or where to look would be greatly
appreciated.

Thank you

Terry
 
J

John W. Krahn

Terry said:
I am a few hours into my life after Perl.

I need to rename approximately 5000 .jpg files, which I think I have
figured out thanks to "Learning Perl" 3rd. Ed. (pg. 176).

Unlike that example, I would like to compare/match the file names to a
table/field in a csv file and once matched rename the file to the
second table/field in the csv file so that ABCDEFG.jpg becomes
1111111.jpg and ABCDEFH.jpg becomes 1111112.jpg and so on.

I've been searching my perldoc, comp.lang.perl.misc and "Learning Perl"
most of the day and haven't found anything that looks like what I want
to do, but I'm chalking that up to not knowing where to look.

Any suggestions of howto: or where to look would be greatly
appreciated.

UNTESTED:


open my $csv, '<', 'csv_file' or die "Cannot open 'csv_file' $!";

while ( <$csv> ) {
chomp;
my ( $from, $to ) = split /,/;
if ( -e $from and not -e $to ) {
rename $from, $to or warn "Cannot rename '$from' $!";
}
}

close $csv;




John
 
B

Ben Morrow

Quoth "John W. Krahn said:
UNTESTED:

And wrong.
open my $csv, '<', 'csv_file' or die "Cannot open 'csv_file' $!";

while ( <$csv> ) {
chomp;
my ( $from, $to ) = split /,/;

CSV is actually quite complicated for such a simple format, and this is
not the right way to parse it. Use a module, such as Text::CSV_XS.
if ( -e $from and not -e $to ) {
rename $from, $to or warn "Cannot rename '$from' $!";
}
}

close $csv;

If you're not going to check the return value, there's no point doing an
explicit close.

Ben
 
A

axel

Terry said:
I am a few hours into my life after Perl.
I need to rename approximately 5000 .jpg files, which I think I have
figured out thanks to "Learning Perl" 3rd. Ed. (pg. 176).
Unlike that example, I would like to compare/match the file names to a
table/field in a csv file and once matched rename the file to the
second table/field in the csv file so that ABCDEFG.jpg becomes
1111111.jpg and ABCDEFH.jpg becomes 1111112.jpg and so on.
Any suggestions of howto: or where to look would be greatly
appreciated.

If I understand correctly, you have a CSV file based on two filenames (and
maybe some extra fields?) and need to take your list of filenames, look
up and old name if if in the CSV list, rename it to a new name from the
same record in the CSV list.

Others have mentioned CPAN modules to handle CSV data. Although if
your data are simply filenames separated by a comma with no fields
specified in quotation marks, then it may be easier to simply split
the incoming records... but only if you are totally sure of your data
source.

Whichever method you choose, you can then create a hash based on a key
of the 'old' filename and value of the 'new' filename.

It is then trivial to check incoming filenames to see if they are in the
hash and perform any required renaming, logging and so on.

Axel
 
T

Terry

If I understand correctly, you have a CSV file based on two filenames (and
maybe some extra fields?) and need to take your list of filenames, look
up and old name if if in the CSV list, rename it to a new name from the
same record in the CSV list.

Others have mentioned CPAN modules to handle CSV data. Although if
your data are simply filenames separated by a comma with no fields
specified in quotation marks, then it may be easier to simply split
the incoming records... but only if you are totally sure of your data
source.

Whichever method you choose, you can then create a hash based on a key
of the 'old' filename and value of the 'new' filename.

It is then trivial to check incoming filenames to see if they are in the
hash and perform any required renaming, logging and so on.

Axel

Axel,
You are correct understanding what I am saying.
My data file contains name, quasi-random letters (ROT'ed from the ID),
and an ID number.
If I understand you correctly, I should use split to read my file then
use a hash to rename my files? I will try it this way.

My appologies for the 5 days inbetween posts, but I went on vacation
and unplugged for a few days.

Thank you

Terry
 
J

Josef Moellers

Terry said:
My appologies for the 5 days inbetween posts, but I went on vacation
and unplugged for a few days.

As in "I'll post this question now, then go on a vacation. Let them sort
this out, discuss the pro's and con's and when I'm back, I can pick up a
solution, without having participated in the discussion, shown what I
tried and where it failed, rectified false assumptions"?

26 to 31 ist exactly 5 days.
 
T

Terry

Wrong.
I was not planning taking a long weekend before I posted, but that's
what ended up happening. I didn't have to mention it, but I felt that
I should in consideration of the people trying to help me.
Thanks for your help.

Terry
 
T

Ted Zlatanov

As in "I'll post this question now, then go on a vacation. Let them
sort this out, discuss the pro's and con's and when I'm back, I can
pick up a solution, without having participated in the discussion,
shown what I tried and where it failed, rectified false assumptions"?

26 to 31 ist exactly 5 days.

That was uncalled for, especially since Terry apologized.

Ted
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top