Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

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
[Corion]: Oh, poor Trump can't get a break. Even the Vatican only sends the second in command to greet him...
[shmem]: Discipulus: readdir has the same semantics as readline (a.k.a <>). Have another coffee ☕ ;-)
[Discipulus]: ah! just with my old system perl (5.8.8) was needed
[Corion]: Has anybody toyed around with Mastodon? There is a Perl module for it, but I'm not sure that it's a yak worth chasing
[Corion]: On the upside, maybe running that for my family would be interesting, or at least for myself
[Corion]: I'm not fond of using a server hosted by other people, even if they claim to be GNU ;)
[Discipulus]: gee a social network with the family.. is this a dread? ;=)

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2017-05-26 07:35 GMT
Find Nodes?
    Voting Booth?