#1 2019-10-16 13:48

luisxv2k
Member
Registered: 2019-10-16
Posts: 2

Rename if first 7 positions equal to the next 7 positions

Hi den4b and Team!

I have a folder with several subfolders and within the subfolders several files with different names.

I need to rename files that have the first 7 positions equal to the next 7 positions.
Example:
   A file named "SBAC - SBAC - ADC.pdf" should be renamed to "SBAC - ADC.pdf".
   A file named "SBBE - SBBE - PDC.pdf" should be renamed to "SBBE - PDC.pdf".

How to solve it?
Thanks for your support!

Admin note: Your post was moved from topic #2107. No necroposting, as per forum rules.

Last edited by den4b (2019-10-16 19:19)

Offline

#2 2019-10-16 16:05

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

Re: Rename if first 7 positions equal to the next 7 positions

Hi and welcome luisxv2k.

Your issue (rename if double start at name begin) is different from the issue in this topic (rename from file content), so these posts may be moved to a new dedicated thread.

- - -

FROM:
ABBA - SBBE - PDC.pdf
SBAC - SBAC - ADC.pdf     < doubled part 'SBAC - '
SBBE - SBBE - PDC.pdf
YEAR - SBAC - ADC.pdf
TO:
ABBA - SBBE - PDC.pdf
SBAC - ADC.pdf       < doubled part removed
SBBE - PDC.pdf
YEAR - SBAC - ADC.pdf


TRY this
PascalScript (see http://www.den4b.com/wiki/ReNamer:Rules:PascalScript)

var
t1,t2,t3:WideString;
amount:Integer;

begin
  amount:=7;
  //WideCopy(String, Index, Count)
  t1 := WideCopy(FileName,1,amount);        //first  7:    start from pos 1, take 7
  t2 := WideCopy(FileName,amount+1,amount); //second 7:    start from pos 8, take 7
  t3 := WideCopy(FileName,amount+1,999);    //wanted name: start from pos 8, take till the end
  //ShowMessage( t1 +#13+ t2 +#13+ t3);

  If(t1=t2) Then FileName := t3;
end.


HTH? big_smile


For more help and how-to's  read our fine Wiki.
If ReNamer had helped you, please donate a coin to Denis or buy a Pro license for commercial use. (Lite vs Pro)

Offline

#3 2019-10-17 12:02

luisxv2k
Member
Registered: 2019-10-16
Posts: 2

Re: Rename if first 7 positions equal to the next 7 positions

Hi Stefan,
Thank you so much for the quick return!
I applied this code to the files and it worked perfectly!
Thank you again!

Offline

#4 2019-10-22 17:27

den4b
Administrator
From: den4b.com
Registered: 2006-04-06
Posts: 2,849

Re: Rename if first 7 positions equal to the next 7 positions

You can also use the power of back references in the Regular Expressions rule, like follows:

Replace expression "(\w+)\s+\-\s+\1" with "$1" (skip extension)

Example input:

SBBI - SBBI - ADC.pdf
SBBP - SBBP - VAC.pdf
SBBQ - SBBQ - ADC.pdf
SBBR - SBBR - ADC.pdf
SBBV - SBBV - PDC.pdf
SBCA - SBCA - VAC.pdf

Output:

SBBI - ADC.pdf
SBBP - VAC.pdf
SBBQ - ADC.pdf
SBBR - ADC.pdf
SBBV - PDC.pdf
SBCA - VAC.pdf

Offline

#5 2019-10-23 09:36

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

Re: Rename if first 7 positions equal to the next 7 positions

den4b wrote:

You can also use the power of back references in the Regular Expressions rule


Thank you, I tried that at first, but failed on the syntax. Good to know that this way works also.





 


For more help and how-to's  read our fine Wiki.
If ReNamer had helped you, please donate a coin to Denis or buy a Pro license for commercial use. (Lite vs Pro)

Offline

Board footer

Powered by FluxBB