#1 2009-12-18 18:31

ThanhLoan
Member
Registered: 2009-12-17
Posts: 17

RegEx to extract first letter of every words in a song name

Hi,
I follow you all from the BRU forum and I switched to Renamer as well.
I would like to ask for help in the following areas :

1. I might be wrong but Renamer seems to have max of 9 groups of replacement. When I have more than 9, I can't specify the group .
exp : I can't write $10 ; Renamer will take it as group 1 folowed by a '0'
2. My request is for adding abbreviation of songname in a karaoke filename.

Example :
my original karaoke filename :
SunFly116_12-Singer Name-Aname Bname Cname Dname Ename Fname Gname.mkv

I want to rename it to :
SunFly116_12-Singer Name-Aname Bname Cname Dname Ename Fname Gname-ABCDEFG.mkv

This is the RegEx :
(.*)(([A-Z])[a-z]* ([A-Z])[a-z]* ([A-Z])[a-z]* ([A-Z])[a-z]* ([A-Z])[a-z]* ([A-Z])[a-z]* ([A-Z])[a-z]*)
Replace : $1$2-$3$4$5$6$7$8$9

However, this RegEx only works with a songname of 7 words exactly (Aname to Gname) and I don't know how to make it work for any number of words in the songname.

Please advise

NB : I am glad Renamer supports Unicode...

Offline

#2 2009-12-18 20:18

Stefan
Moderator
From: Germany, EU
Registered: 2007-10-23
Posts: 1,161

Re: RegEx to extract first letter of every words in a song name

ThanhLoan wrote:

Hi,
I follow you all from the BRU forum and I switched to Renamer as well.

2. My request is for adding abbreviation of songname in a karaoke filename.

Example :
my original karaoke filename :
SunFly116_12-Singer Name-Aname Bname Cname Dname Ename Fname Gname.mkv

I want to rename it to :
SunFly116_12-Singer Name-Aname Bname Cname Dname Ename Fname Gname-ABCDEFG.mkv

Hi ThanhLoan, welcome. Nice to have you here!

As i understood you correct you want to rename
FROM:
SunFly116_12-Singer Name-Aname Bname Cname Dname Ename.mkv
SunFly116_12-Singer Name-Aname Bname Cname Dname Ename Fname Gname.mkv
SunFly116_12-Singer Name-Aname Bname Cname Dname Ename Fname Gname Hname Iname.mkv
TO:
SunFly116_12-Singer Name-Aname Bname Cname Dname Ename-ABCDE.mkv
SunFly116_12-Singer Name-Aname Bname Cname Dname Ename Fname Gname-ABCDEFG.mkv
SunFly116_12-Singer Name-Aname Bname Cname Dname Ename Fname Gname Hname Iname-ABCDEFGHI.mkv

SO please try this script:
(See Wiki how to handle this script: http://den4b.com/wiki/ReNamer:Rules:PascalScript)

//Take First char from each word and collect them to the end of filename. Stefan 12/2009
//FROM: SunFly116_12-Singer Name-Aname Bname Cname Dname Ename Fname Gname.mkv
//TO:   SunFly116_12-Singer Name-Aname Bname Cname Dname Ename Fname Gname-ABCDEFG.mkv
// See Wiki how to handle this script: http://www.den4b.com/wiki/ReNamer:Rules:PascalScript

var
  I, SplitPos: Integer;
  Parts: TStringsArray;
  FirstChar, FirstChars: WideString;
  
begin
  //Split filename into parts at each space:
  Parts := WideSplitString(WideExtractBaseName(FileName), ' ');
  
  //Parts0 holds "SunFly116_12-Singer".
  
  //Parts1 holds "Name-Aname"
  //So special handling of this second part is needed.
  //We split this part again at the dash:
  SplitPos := Pos('-', Parts[1]);
  FirstChars := Copy(Parts[1],SplitPos+1,1);
  
  //Parts2 till Partsn holds the rest:
  for I:=2 to Length(Parts)-1 do
  begin
    FirstChar := Copy(Parts[i],1,1);
    FirstChars := FirstChars + FirstChar;
  end;
  
  //Test:
  ShowMessage(WideExtractBaseName(FileName)+'-'+FirstChars+WideExtractFileExt(FileName));
  
  //Enable renaming:
  //FileName := WideExtractBaseName(FileName)+'-'+FirstChars+WideExtractFileExt(FileName);
end.

HTH?  big_smile
If Yes, please help two others too and spread the word of ReNamer


Read the  *WIKI* for HELP + MANUAL + Tips&Tricks.
If ReNamer had helped you, please *DONATE* to Denis or buy a PRO license. (Read *Lite vs Pro*)

Offline

#3 2009-12-18 20:38

ThanhLoan
Member
Registered: 2009-12-17
Posts: 17

Re: RegEx to extract first letter of every words in a song name

Thanks so much Stefan for your quick reply.
I apologize for knowing nothing about Pascal so I paste this scrip into a PascalScript rule and get this error message
[Line 16] : Duplicate Identifier 'I'
Please advise

Offline

#4 2009-12-18 20:51

ThanhLoan
Member
Registered: 2009-12-17
Posts: 17

Re: RegEx to extract first letter of every words in a song name

OK Stefan,
I succeeded to compile the script but I think I misexplain my needs :
I'd just like to add the first letter of every word in the song name which is after the 2nd dash (-)
The script ran OK but did not rename the files
When I click Preview, I got a popup message from Renamer showing the new file name which does not correspond to what I try to achieve..

Please advise

Offline

#5 2009-12-18 21:03

ThanhLoan
Member
Registered: 2009-12-17
Posts: 17

Re: RegEx to extract first letter of every words in a song name

Hi Stefan,
To be more precise, I would liike to take the first letter of every word between the 2nd dash (-) and the extension of the file (.mkv). The file names are in Unicode.
Example :
From : Asia Karaoke 49_14-Chí Tâm+Ngọc Huyền-Tạ Từ Trong Đêm Tân Cổ Giao Duyên.mkv
To :     Asia Karaoke 49_14-Chí Tâm+Ngọc Huyền-Tạ Từ Trong Đêm Tân Cổ Giao Duyên-TTTDTCGD.mkv

Thank you

Offline

#6 2009-12-18 22:25

Andrew
Senior Member
Registered: 2008-05-22
Posts: 542

Re: RegEx to extract first letter of every words in a song name

ThanhLoan, try this and see if it works:

var
  i: Integer;
  Parts: TStringsArray;
  FirstChars: WideString;

begin
  Parts := WideSplitString(ReplaceRegEx(WideExtractBaseName(FileName), '.*-', '', false, false), ' ');
  for i := 0 to Length(Parts)-1 do
    FirstChars := FirstChars + Copy(Parts[i], 1, 1);
  FileName := WideExtractBaseName(FileName)+'-'+FirstChars+WideExtractFileExt(FileName);
end.

Press Preview first and if you're satisfied, press Rename.

Offline

#7 2009-12-18 22:29

Stefan
Moderator
From: Germany, EU
Registered: 2007-10-23
Posts: 1,161

Re: RegEx to extract first letter of every words in a song name

> When I click Preview, I got a popup message
My script show an Message only for testing purpose.
You have to comment the "WideShowMessage"-line and un-comment the "FileName"-Line

>Example :
>From : Asia Karaoke 49_14-Chí Tâm+Ngọc Huyền
Ahh, now you provide an real example, which differs from your first examples.
Since my first script takes spaces into account, the script didn't works whit your new example file name.

> I would liike to take the first letter of every word between the 2nd dash (-) and the extension of the file (.mkv).
You are right. That's an better approach.

So please try this rewritten script:

//Take First char from each word and collect them to the end of filename. Stefan 12/2009
//FROM: SunFly116_12-Singer Name-Aname Bname Cname Dname Ename Fname Gname.mkv
//TO:   SunFly116_12-Singer Name-Aname Bname Cname Dname Ename Fname Gname-ABCDEFG.mkv
// See Wiki how to handle this script: http://www.den4b.com/wiki/ReNamer:Rules:PascalScript

var
  I, SplitPos: Integer;
  Parts: TStringsArray;
  Base, FirstChar, FirstChars, Temp: WideString;
  
begin
    Base := WideExtractBaseName(FileName)
    SplitPos := Pos('-', Base);
    Temp := WideCopy(Base,SplitPos+1,999); //first '-'
    SplitPos := Pos('-', Temp);
    Temp := WideCopy(Temp,SplitPos+1,999); //second '-'
  //WideShowMessage(temp);
    
  Parts := WideSplitString(Temp, ' ');
  
  for I:=0 to Length(Parts)-1 do
  begin
    FirstChar := WideCopy(Parts[i],1,1);
    FirstChars := FirstChars + FirstChar;
  end;
  
  //Test:
  WideShowMessage(Base+'-'+FirstChars+WideExtractFileExt(FileName));
  
  //Enable renaming:
  //FileName := Base+'-'+FirstChars+WideExtractFileExt(FileName);
  FirstChars := '';
end.

ReNamer_ExtractFirstChar001.PNG

Last edited by Stefan (2009-12-18 22:57)


Read the  *WIKI* for HELP + MANUAL + Tips&Tricks.
If ReNamer had helped you, please *DONATE* to Denis or buy a PRO license. (Read *Lite vs Pro*)

Offline

#8 2009-12-19 00:11

ThanhLoan
Member
Registered: 2009-12-17
Posts: 17

Re: RegEx to extract first letter of every words in a song name

You are the champion Stefan..Thank you so much...it works perfectly.
You just saved me 20 hours of manual renaming...

Have wonderful Holiday Seasons

Offline

#9 2009-12-20 01:29

Andrew
Senior Member
Registered: 2008-05-22
Posts: 542

Re: RegEx to extract first letter of every words in a song name

ThanhLoan, I know you got your problem solved by Stefan's wonderful script, but for the sake of people reading this thread later, can you confirm whether my solution works as well? It's always nice to have alternate solutions to the same problem, since one might be easier to tweak compared to another if someone has a slightly similar request.

Offline

#10 2009-12-21 22:02

ThanhLoan
Member
Registered: 2009-12-17
Posts: 17

Re: RegEx to extract first letter of every words in a song name

Hi Andrew,
I am so sorry I did not reply to you.
You are so nice to provide me the script and I thought it came from Stefan as well.
I did test your script and effectively, it works seamlessly as well.
You are very knowledgeable and I am very happy be partcipate in this forum.

Once again, my apologies and my sincere thanks to Andrew and Stefan.

Offline

Board footer

Powered by FluxBB