# den4b Forum

You are not logged in.

## #1 2017-05-31 12:46

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

### ReNumber rule: Increase / Add , Decrease / Substract number digits

Q: how can I do the math and add amount of x to existent numbers?

FROM:
Test 3 2017.txt
Test 5 2017.txt
Test 9 2017.txt

TO:
Test 6 2017.txt
Test 8 2017.txt
Test 12 2017.txt

Possible GUI for a new ReNumber rule:

Work on found number #: 1
[_] Right-to-left

Math amount: 3
[_] Allow negative result (else set to '0')

- - -

Explanation

The Rule should detect any group of coherent digits in a file name,
no matter how they are delimited, by space, hyphen, underscore, dot, char or any other sign.

Examples:
Letter 32.doc (NaN, Space, Number)
Letter 4 567 Draft.doc (NaN, Space, Number 1, Space, Number 2, Space, NaN)
Letter 2017-06-02 Draft.doc (NaN, Space, Number 1, Hyphen, Number 2, Hyphen, Number 3, Space, NaN)

The user tell the rule on which of the dedected numbers he want to work:
Work on found number #: 1

To make life easier, let the user declare wanted number counted from the right:
[_] Right-to-left

Next he tell how he wants to calculate; increase, decrease or replace by a fix number, used for every file:
Math amount: 3
[_] Allow negative result (like '-4', else set to '0')

Next he tell which minimum number length he wants.
If the result would be shorter, zeros would be added to the left.
If the result is bigger as the wanted length, then that is fine too, no cutting down.

- - -

Maybe we have to add a rule before to split number into wanted parts first (and rule after to remove the split again)

Original:
20170523064530.txt

Step 1: Rule to split by RegEx and add underscore to extract wanted part:
20170523_06_4530.txt

Step 2: Rule to do the math (new suggested rule):

Work on found number #: 2           [_] Right-to-left

20170523_07_4530.txt

Step 3: Rule to remove underscore again:
20170523074530.txt

Done.

- - -

##### ################ PascalScript ################

In the mean time I have handicrafts a script for that issue.

"//// U S E R   S E T T I N G S" need to be adjusted in script for each different renaming task:
- RegEx pattern: Split file name into two or three parts, strRE := '(\d{8})(\d{2})(.+)';
- WorkOnPart: On which part of the split file name do you want to work, intPart := 2;
- Wanted amount to add / increase (or to substract / decrease), intAmountToCalc := 1;
- Math, do you want to add or to subtract, strDirection := 'add';
- Wanted length of the new number (pad to length), intNewNumberLength := 2;
- EDIT: Version 17.05.31b, added boolAllowNegativeResult := false

The code should handle the rest for you automatically (as far as I could think of)

Please test first and use on own risk.  ---  See examples in script.

(See our wiki for an how to using rules and scripts ReNamer:Rules:PascalScript)

``````////ReNumber_Increase_Decrease_Add_Substract_Digits.pas
////PascalScript for den4b's ReNamer (http://www.den4b.com/projects.php),
//// 2010-2019 by Stefan, Version 19.01.27
//// Version 19.01.27 : added better debug info and added possibility to insert a string to prevend duplicated names.
//// Found at: http://www.den4b.com/forum/viewtopic.php?pid=9982#p9982
////Purpose: Find number in file name and increase/decrease them.

////	!!! Adjust the script at "//U S E R   S E T T I N G S" to explain your needs	!!!

////EXAMPLE 1
// // //  FROM (here YYYYmmDDhhMMss.txt):
// // //  20170523064530.txt
// // //  20170523082330.txt
// // //  20170523101730.txt
// // //  TO (here increase hour by one each):
// // //  20170523074530.txt
// // //  20170523092330.txt
// // //  20170523111730.txt
// // //  USER Settings:
// // //  strRE := '(.{8})(.{2})(.+)';  //Split file name into two or three parts
// // //  intPart := 2; //Work on second part of split, here on "(.{2})" == 'hh'
// // //  intAmountToCalc := 1;  //Wanted amount to add or to subtract
// // //  intNewNumberLength := 2;  //Pad to minimum length of 2 digits, add zeros if need

////EXAMPLE 2
// // //  FROM:
// // //  786_red_socks_01.jpg
// // //  786_red_socks_04.jpg
// // //  786_red_socks_05.jpg
// // //  873_red_socks_12.jpg
// // //  873_red_socks_14.jpg
// // //  873_red_socks_25.jpg
// // //  786_red_socks_0051.jpg
// // //  786_red_socks_0054.jpg
// // //  786_red_socks_0055.jpg
// // //  873_red_socks_0062.jpg
// // //  873_red_socks_0064.jpg
// // //  873_red_socks_0075.jpg
// // //  USER Settings:
// // //  strRE := '(.+)(\d{2})';
// // //  intPart := 2; //Work on second part
// // //  intAmountToCalc := 25;
// // //  strDirection := 'add';
// // //  intNewNumberLength := 4;

////EXAMPLE 3
// // //  FROM:
// // //  Magazine_01.jpg
// // //  Magazine_04.jpg
// // //  Magazine_05.jpg
// // //  Magazine_12.jpg
// // //  Magazine_214.jpg
// // //  Magazine_1325.jpg
// // //  Magazine_0091.jpg
// // //  Magazine_0094.jpg
// // //  Magazine_0095.jpg
// // //  Magazine_0102.jpg
// // //  Magazine_0304.jpg
// // //  Magazine_1415.jpg
// // //  USER Settings:
// // //  strRE := '(.+_)(\d+)';
// // //  intPart := 2; //Work on second part
// // //  intAmountToCalc := 90;
// // //  strDirection := 'Add';
// // //  intNewNumberLength := 4;

////EXAMPLE 4
// // //  FROM:
// // //  Counter 5.pdf
// // //  Counter 25.pdf
// // //  Counter 50.pdf
// // //  Counter 2555.pdf
// // //  TO (substract 7):
// // //  Counter 0.pdf
// // //  Counter 18.pdf
// // //  Counter 43.pdf
// // //  Counter 2548.pdf
// // //  USER Settings:
// // //  strRE := '(.+?)(\d+)';
// // //  intPart := 2; //Work on second part
// // //  intAmountToCalc := 7;
// // //  strDirection := 'Sub';
// // //  intNewNumberLength := 0;
//
// // //  TO (negative result possible):
// // //  Counter -2.pdf
// // //  Counter 18.pdf
// // //  Counter 43.pdf
// // //  Counter 2548.pdf
// // //  Use:
// // //  boolAllowNegativeResult := true

////EXAMPLE 5
// // //  FROM:
// // //  Vacation2015 - 1 - Lake Mounty Route 66.jpg
// // //  Vacation2015 - 2 - Lake Mounty Route 66.jpg
// // //  Vacation2015 - 3 - Lake Mounty Route 66.jpg
// // //  Vacation2015 - 4 - Lake Mounty Route 66.jpg
// // //  Vacation2015 - 00100 - Lake Mounty Route 66.jpg
// // //  Vacation2015 - 00101 - Lake Mounty Route 66.jpg
// // //  Vacation2015 - 00102 - Lake Mounty Route 66.jpg
// // //  Vacation2015 - 00103 - Lake Mounty Route 66.jpg
// // //  USER Settings:
// // //  strRE := '(.+ - )(\d+)( -.+)';
// // //  intPart := 2; //Work on second part
// // //  intAmountToCalc := 99;
// // //  strDirection := 'add';
// // //  intNewNumberLength := 5;

////EXAMPLE 6
// // //  FROM:
// // //  8 Example 1234.ext
// // //  9 Example 1234.ext
// // //  10 Example 1234.ext
// // //  11 Example 1234.ext
// // //  9 Example 1234.ext
// // //  10 Example 1234.ext
// // //  11 Example 1234.ext
// // //  12 Example 1234.ext
// // //  USER Settings:
// // //  strRE := '(\d+)( .+)';
// // //  intPart := 1; //Work on second part
// // //  intAmountToCalc := 1;
// // //  strDirection := 'add';
// // //  intNewNumberLength := 0;
//// /END of examples.

/// T H E    S C R I P T
//////////////////////////////    //////////////////////////////    //////////////////////////////    //////////////////////////////
var
SubPatterns: TStringsArray;
intOldNumber,intPart,intAmountToCalc,intNewNumberLength,intCalculation: Integer;
strRE,strDirection,strNewNumber,strNewName,strDuplicate: String;
boolDebugOutname,boolAllowNegativeResult,boolDebugShowmessage: Boolean;

Begin

//// U S E R   S E T T I N G S
//////////////////////////////

//// RegEx pattern:
//// Split file name (without the extension)  into parts (needs to be adjusted by user depending on file name pattern):
//// 	Split into two -or- three parts (base name only, no extension)
//// 		Two parts: (old number)( and the rest)
//// 		Two parts: (Before and )(old number)
//// 		Three parts: (Before and )(old number)( and the rest)

//     LName (PName) 19-200-01.txt
strRE := '^(.+ )(\d\d)(-\d\d\d-\d\d)\$';

//// WorkOnPart:
//// On which part of the split file name do you want to work:
intPart := 2;

////Wanted amount to add / increase (or to subtract / decrease):
intAmountToCalc := 3;

////Math, do you want to add or to subtract:
/// use "add" -or- "sub" -or- "fix":

////Wanted length of the new number, taken from the right, add zero on the left if need:
////(Use "intNewNumberLength := 0;" to disable this feature)
intNewNumberLength := 0;

////Add underscore signs before and after new number for better identifying in the preview?
//// True: Yes, please add them. // False: No, not need at all.
boolDebugOutname := False;

////Allow Negative Result?
//// True: Yes, please allow (e.g.: -3). // False: No, use zero '0' instead.
boolAllowNegativeResult := false;

////Show message box with details from current step in code:
boolDebugShowmessage :=false;

////Prevent duplicated file names, add a string, remove that afterwards yourself in a second run:
//// (Use a sign or string which is not a part of existent file names)
strDuplicate := '#'

//// /END of user settings

//// T H E   C O D E
//////////////////////////////
//// -------------------------------------------------------------------
////Split file name into parts (needs to be adjusted by user depending on file name pattern):
////function SubMatchesRegEx(const Input, Find: WideString; const CaseSensitive: Boolean): TWideStringArray;
SubPatterns:=SubMatchesRegEx(WideExtractBaseName(FileName),strRE,false);
////Debug message:
IF(boolDebugShowmessage) Then ShowMessage('Amount of found parts: ' + IntToStr(Length(SubPatterns) ));

If Length(SubPatterns) <=0 then exit;

////Work with found parts (Parts counting start at zero: Part0, Part1, Part2,...):
intPart := intPart -1 ;	////decrease by one to translate from user wish to array rule.
If intPart < 0 Then
intPart := 0;
intOldNumber := StrToInt(SubPatterns[intPart]);

////Debug message:
IF(boolDebugShowmessage) Then ShowMessage('Content of part: ' +  IntToStr(intOldNumber));

//// -------------------------------------------------------------------
////Do the wanted configuration (use ONE of this three alternatives):
////IsMatchingRegEx(const Input, Pattern: WideString; const CaseSensitive: Boolean): Boolean;
If IsMatchingRegEx(strDirection,'sub', False)  Then	  //// substract / decrease (- intAmountToCalc):
intCalculation := intOldNumber - intAmountToCalc;
intCalculation := intOldNumber + intAmountToCalc;
If IsMatchingRegEx(strDirection,'fix', False) Then	  //// add as fix number
intCalculation := intAmountToCalc;

////Allow negative result (like -3) or not (use always '0' instead)?:
If boolAllowNegativeResult = False Then
If intCalculation < 0 Then intCalculation := 0;

If intNewNumberLength > 0 Then
begin
If Length(IntToStr(intCalculation)) < intNewNumberLength Then
begin
strNewNumber := '000000000' + IntToStr(intCalculation);
////Take the wanted amount of digits from the right:
strNewNumber := WideCopy(strNewNumber, Length(strNewNumber) -(intNewNumberLength-1), intNewNumberLength);
////move minus/hyphen to the front:
If Pos( '-',strNewNumber) > 0 Then strNewNumber := '-0' + ReplaceRegEx(strNewNumber,'-','',False,False);
end
Else
strNewNumber := IntToStr(intCalculation);
end
Else
strNewNumber := IntToStr(intCalculation);

If boolDebugOutname = TRUE Then strNewNumber := '___'+ strNewNumber + '___';
SubPatterns[intPart] := strNewNumber;
//// -------------------------------------------------------------------
////Compose new file name (be sure to add all wanted parts):
If Length(SubPatterns) = 1 then FileName :=  SubPatterns[0] + strDuplicate + WideExtractFileExt(FileName);
If Length(SubPatterns) = 2 then FileName :=  SubPatterns[0] + SubPatterns[1] + strDuplicate + WideExtractFileExt(FileName);
If Length(SubPatterns) = 3 then FileName :=  SubPatterns[0] + SubPatterns[1] + SubPatterns[2] + strDuplicate + WideExtractFileExt(FileName);
End.
//////////////////////////////    //////////////////////////////    //////////////////////////////    //////////////////////////////``````

Thanks for making ReNamer.

If that script helps you, please see my signature if you are able to help Denis too, thanks.

Offline

## #2 2017-06-02 17:16

den4b
From: den4b.com
Registered: 2006-04-06
Posts: 3,164

### Re: ReNumber rule: Increase / Add , Decrease / Substract number digits

Interesting idea!

It has been added as a new feature for future consideration, with a low priority.

Let's see if it attracts attention from other users.

Offline

## #3 2017-07-10 08:41

Stamimail
Member
Registered: 2016-05-08
Posts: 81

### Re: ReNumber rule: Increase / Add , Decrease / Substract number digits

I also needed to do arithmetic operations.
Currently I do it by:
1. Using Excel
2. Add Rule > User Input

Offline

## #4 2017-08-08 23:26

den4b
From: den4b.com
Registered: 2006-04-06
Posts: 3,164

### Re: ReNumber rule: Increase / Add , Decrease / Substract number digits

Stamimail wrote:

I also needed to do arithmetic operations.

Can you list the types of arithmetic operations that you needed to perform?

This will help shape up the rule, when the time comes to implement it.

Offline

## #5 2017-08-08 23:52

Stamimail
Member
Registered: 2016-05-08
Posts: 81

### Re: ReNumber rule: Increase / Add , Decrease / Substract number digits

I think in most cases I needed + or - (than * or /)
Anyway,
I think this kind of implementation will solve any type of query.

Offline