You are not logged in.

#1 2011-09-26 16:29:11

sweetthdevil
Member
Registered: 2009-10-20
Posts: 415

Script to rename tv show with season and episode number

Hi all,

I am working on a script to rename all the files of a tv show contain into a single folder - keeping season & episodes numbers.

I am using zenity to get the correct name of the tv show (mainly to add capital letters, and remove the dots - and also to be able to use the script for different tv show)

then again using zenity to select which folder contain the files, and then I want zenity to display how the rename will do as a verification and if happy then process.

The script work great but I can get zenity to display the output of the command and ask the question.

Any suggestion?

Many thanks,

Regards,

#!/bin/bash
#Use Zenity to capture the title of the TV Show
tv_show_name=$(zenity --entry)
#Select folder where the files to be rename are
sourcefolder=$(zenity --file-selection --directory --title "Please select the folder containing those Files")
#Move into the folder
cd $sourcefolder
#Now we execute the script to keep season and episode numbers
if [ $? == 1 ]; then exit; fi
for filename in *; do
    if [[ "$filename" =~(\**).*(.*[0-9][0-9]).*([0-9][0-9]).*(\....)$ ]]; then
result=$(`echo mv "$filename" "$tv_show_name S${BASH_REMATCH[2]} E${BASH_REMATCH[3]}${BASH_REMATCH[4]}"`)
zenity --question --title="Are those title correct?" \
	--text=$result \

if [[ $? -eq 0 ]] ; then exit; fi
else 
mv "$filename" "$tv_show_name S${BASH_REMATCH[2]} E${BASH_REMATCH[3]}${BASH_REMATCH[4]}")
    fi
done
exit

Offline

#2 2011-09-26 20:15:02

slint
Member
Registered: 2009-05-22
Posts: 31

Re: Script to rename tv show with season and episode number

You should work a bit on your quotation signs

So far what I've seen:

 cd "$sourcefolder"

" is neede if your directory has a space in it

 result=$(echo mv \"$filename\" \"$tv_show_name S${BASH_REMATCH[2]} E${BASH_REMATCH[3]}${BASH_REMATCH[4]}\") 

should let zenity show the titles

Offline

#3 2011-09-27 09:01:49

sweetthdevil
Member
Registered: 2009-10-20
Posts: 415

Re: Script to rename tv show with season and episode number

Yes you're right, but it still doesn't work.

See the terminal output:

[sweetth@myhost ~]$ '/home/sweetth/test-rn.sh' 
/home/sweetth/test-rn.sh: line 18: syntax error near unexpected token `)'
/home/sweetth/test-rn.sh: line 18: `mv "$filename" "$tv_show_name S${BASH_REMATCH[2]} E${BASH_REMATCH[3]}${BASH_REMATCH[4]}")'

Offline

#4 2011-09-27 12:41:46

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Script to rename tv show with season and episode number

You should not be trying to escape the double quotes.

$ for f in \"foo bar baz\"; do echo "$f"; done
"foo
bar
baz"
$ for f in "foo bar baz"; do echo "$f"; done
foo bar baz

Offline

#5 2011-09-27 12:46:05

sweetthdevil
Member
Registered: 2009-10-20
Posts: 415

Re: Script to rename tv show with season and episode number

First of all many thanks for your assistance,

But I am unsure on how to use the code there?

Offline

#6 2011-09-27 12:49:05

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Script to rename tv show with season and episode number

sigh.

result=$(echo mv "$filename" "$tv_show_name S${BASH_REMATCH[2]} E${BASH_REMATCH[3]}${BASH_REMATCH[4]}") 

http://mywiki.wooledge.org/BashGuide

Offline

#7 2011-09-27 14:34:45

sweetthdevil
Member
Registered: 2009-10-20
Posts: 415

Re: Script to rename tv show with season and episode number

Right I kind of understand the principle after reading about "foo bar baz"

But I still don't get your code, and how to display the result using zenity, I am actually more confuse than before?!

Offline

#8 2011-09-27 15:53:51

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Script to rename tv show with season and episode number

The point of the for loops I showed you is to demonstrate word splitting and quoting. I hate to be lame, but the adage 'you have to learn to walk before you can run' seems apropos here.

http://mywiki.wooledge.org/Quotes
http://mywiki.wooledge.org/WordSplitting

Last edited by falconindy (2011-09-27 15:54:00)

Offline

#9 2011-09-27 15:56:45

sweetthdevil
Member
Registered: 2009-10-20
Posts: 415

Re: Script to rename tv show with season and episode number

No I appriciate the need to learn and this is why I play with those script. But I don't get your code.

I will read up your link to try to understand it,

Anyway many thanks for your time and assistance,

Offline

#10 2011-09-27 17:33:47

sweetthdevil
Member
Registered: 2009-10-20
Posts: 415

Re: Script to rename tv show with season and episode number

Right,

I finally got it!

Now the script work (tested with multiple files, different names) and the verification is to a singles files just to be sure not the mess up those episodes number.

#!/bin/bash
#Use Zenity to capture the title of the TV Show
tv_show_name=$(zenity --entry)
#Select folder where the files to be rename are
sourcefolder=$(zenity --file-selection --directory --title "Please select the folder containing those Files")
#Move into the folder
cd "$sourcefolder"
#Now we execute the script to keep season and episode numbers
if [ $? == 1 ]; then 
exit
fi
for filename in *; do
    if [[ "$filename" =~(\**).*(.*[0-9][0-9]).*([0-9][0-9]).*(\....)$ ]]; then
result=$(echo mv \"$filename\" \"$tv_show_name S${BASH_REMATCH[2]} E${BASH_REMATCH[3]}${BASH_REMATCH[4]}\") 
zenity --question --title="Are those title correct?" \
	--text="$result" \

if [[ $? == 0 ]] ; then 
mv "$filename" "$tv_show_name S${BASH_REMATCH[2]} E${BASH_REMATCH[3]}${BASH_REMATCH[4]}"
fi
fi
done
exit

Offline

#11 2011-09-28 06:37:38

satanselbow
Member
Registered: 2011-06-15
Posts: 538

Re: Script to rename tv show with season and episode number

Homework done then? Don't forget to sign that authentic and original work statement tongue

Offline

Board footer

Powered by FluxBB