Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

file compare and renaming

by marco012 (Initiate)
on Apr 20, 2012 at 05:45 UTC ( [id://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 ( wwwS01E01zzz.srt and up to wwwS01E40zzz.srt) . 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 (Archbishop) 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.

    Update:
    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 "$rname.srt" ] then mv "$rname.str" "$aname.str" fi done done

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://966083]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2024-04-19 07:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found