Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

file compare and renaming

by marco012 (Initiate)
on Apr 20, 2012 at 05:45 UTC ( #966083=perlquestion: print w/replies, xml ) Need Help??
marco012 has asked for the wisdom of the Perl Monks concerning the following question:

Hi all! I have a problem that I don't know how to manage with Perl. I have 40 files named as xxxS01E01yyy.avi , xxxS01E02yyy.avi , and so on to 40. I have also 40 subtitles files named in the same way ( and up to . I'm running Linux and my video player accepts subtitles files only if they are named exactly as like as the avi one. So, my question is: how can i automate the renaming process of each srt file? Thank you

Replies are listed 'Best First'.
Re: file compare and renaming
by eyepopslikeamosquito (Chancellor) on Apr 20, 2012 at 10:48 UTC
Re: file compare and renaming
by brx (Pilgrim) on Apr 20, 2012 at 06:53 UTC
    Take a look to rename and perlrun (-n -l -e) (update: and perlretut).
    An adhoc one-liner can do the job.
    Try this...
    ls | perl -nle '$a=$_;s/.*?(S\d\dE\d\d).*?\.(avi|srt)/LikeABoss${1}Yea +h.$2/;print "$a => $_"'
    ... then if everything seems OK (no files with the same "SxxEyy" for example)
    ls | perl -nle '$a=$_;s/.*?(S\d\dE\d\d).*?\.(avi|srt)/LikeABoss${1}Yea +h.$2/;rename $a,$_'
    But don't feel to confident with this : it is a good idea to write a real script if you want to automate this task without human-control. Not my fault if you lose all your pictures/movies/files.

    Add this check.
    Count file before renaming:
    ls | wc -l

    Number of file that should results:
    ls | perl -nle '$a=$_;s/.*?(S\d\dE\d\d).*?\.(avi|srt)/LikeABoss${1}Yea +h.$2/;$h{$_}++}{print scalar keys%h'

    This way you can't lose the files, only the filenames ;-)

      But don't feel to confident with this : it is a good idea to write a real script ...

      :)I'd say that is good advice , write a real script that doesn't rename files :)

      Over the years i've written dozens of renaming scripts, and not doing any actual renaming is the best advice I've got :) Generate a list of moves, review the moves (check collisions, orphans ...), and keep backups

Re: file compare and renaming
by Anonymous Monk on Apr 20, 2012 at 05:58 UTC
Re: file compare and renaming
by sundialsvc4 (Abbot) on Apr 20, 2012 at 12:37 UTC

    I really like my foot.   Nothing beats it for walking around on, so I try to keep it in good shape.   In this case ... having determined for some reason that I did not want to “just do it by hand and be done” for a mere 40 files ... :-/ ... I would write the script to copy the files from one directory into another, renaming them as it went.   (I would begin the script methodically, first merely printing the old and new names to be used, etc.)   But I would never rename or otherwise touch the original files.

    When I know that I am dealing with precious and irreplaceable things, I always want to construct a fully repeatable process.   I would run the script, perfect it, empty the target directory, run it one more time ... then neatly tuck the original directory away just as it originally was and fuhgeddaboudit.   (Disk space is cheap; data isn’t.)

Re: file compare and renaming
by JavaFan (Canon) on Apr 20, 2012 at 13:20 UTC
    40 you said? I'd use the shell:
    trap exit ERR for i in 0 1 2 3 4 do for j in 0 1 2 3 4 5 6 7 8 9 do aname="xxxS01E${i}${j}yyy" rname="wwwS01E${i}${j}yyy" if [ -f "$aname.avi" -a -f "$" ] then mv "$rname.str" "$aname.str" fi done done

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://966083]
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2017-01-20 08:59 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (174 votes). Check out past polls.