Using an input/csv file to rename files

Discussion in 'Perl Misc' started by Terry, Jul 26, 2006.

  1. Terry

    Terry Guest

    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
    Terry, Jul 26, 2006
    #1
    1. Advertising

  2. 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
    John W. Krahn, Jul 27, 2006
    #2
    1. Advertising

  3. Terry

    Ben Morrow Guest

    Quoth "John W. Krahn" <>:
    > 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. ]
    Ben Morrow, Jul 27, 2006
    #3
  4. Terry

    Guest

    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.


    > 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
    , Jul 27, 2006
    #4
  5. Terry

    Terry Guest

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

    >
    > > 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
    Terry, Jul 31, 2006
    #5
  6. 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
    Josef Moellers, Aug 1, 2006
    #6
  7. Terry

    Terry Guest

    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
    Terry, Aug 1, 2006
    #7
  8. Terry

    Ted Zlatanov Guest

    On 1 Aug 2006, 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
    Ted Zlatanov, Aug 1, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michal Mikolajczyk
    Replies:
    0
    Views:
    630
    Michal Mikolajczyk
    Feb 13, 2004
  2. Tintin92
    Replies:
    1
    Views:
    1,675
    Andrew Thompson
    Feb 14, 2007
  3. =?iso-8859-1?b?cultaQ==?=

    Rename multiple files using names in a text file

    =?iso-8859-1?b?cultaQ==?=, Sep 14, 2007, in forum: Python
    Replies:
    2
    Views:
    457
    =?iso-8859-1?b?cultaQ==?=
    Sep 15, 2007
  4. jliu66
    Replies:
    0
    Views:
    493
    jliu66
    Oct 19, 2007
  5. Li Chen
    Replies:
    18
    Views:
    645
    Azmi Farih
    Mar 23, 2010
Loading...

Share This Page