Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Using an input/csv file to rename files

Reply
Thread Tools

Using an input/csv file to rename files

 
 
Terry
Guest
Posts: n/a
 
      07-26-2006
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

 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      07-27-2006
Terry wrote:
> 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
--
use Perl;
program
fulfillment
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      07-27-2006

Quoth "John W. Krahn" <(E-Mail Removed)>:
> Terry wrote:
> > 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:


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

--
Heracles: Vulture! Here's a titbit for you / A few dried molecules of the gall
From the liver of a friend of yours. / Excuse the arrow but I have no spoon.
(Ted Hughes, [ Heracles shoots Vulture with arrow. Vulture bursts into ]
'Alcestis') [ flame, and falls out of sight. ] http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
axel@white-eagle.invalid.uk
Guest
Posts: n/a
 
      07-27-2006
Terry <(E-Mail Removed)> wrote:
> 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


 
Reply With Quote
 
Terry
Guest
Posts: n/a
 
      07-31-2006

(E-Mail Removed) wrote:
> Terry <(E-Mail Removed)> wrote:
> > 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


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

 
Reply With Quote
 
Josef Moellers
Guest
Posts: n/a
 
      08-01-2006
Terry wrote:

> 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.

--
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

 
Reply With Quote
 
Terry
Guest
Posts: n/a
 
      08-01-2006
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

> 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.
>
> --
> Josef Möllers (Pinguinpfleger bei FSC)
> If failure had no penalty success would not be a prize
> -- T. Pratchett


 
Reply With Quote
 
Ted Zlatanov
Guest
Posts: n/a
 
      08-01-2006
On 1 Aug 2006, (E-Mail Removed) wrote:

Terry wrote:
>
>> 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.


That was uncalled for, especially since Terry apologized.

Ted
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Rename File Using Strring Found in File? He Who Greets With Fire Perl Misc 19 03-07-2008 10:01 PM
Rename files using directory names Peter Jamieson Perl Misc 4 11-16-2007 02:25 AM
Rename multiple files using names in a text file =?iso-8859-1?b?cultaQ==?= Python 2 09-15-2007 01:14 PM
How do I rename a group of files using a list of predefined names? cedarson@gmail.com Computer Support 5 08-15-2006 08:45 PM
Rename file names at once (200 files) Adi Computer Support 1 09-09-2004 05:16 PM



Advertisments