#1 2011-02-17 22:50

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

Calculate and increase number found in filename

Oyvindtr wrote:

... got names like this: 12000.jpg, 12006.jpg and so on.
What i need, is to add a fixed number to the filename, by adding it together.

Like this:

Add 61000 to all filenames:
12000.jpg --> 73000.jpg
12006.jpg --> 73006.jpg


Can anyone tell me how to do this?
Thanks!



rename reckoning reckonings add increase decrease digits number calculate calculation numeral decal count from file name


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

#2 2011-02-17 23:04

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

Re: Calculate and increase number found in filename

Add 61000 to all filenames:
FROM:
12000.jpg
12006.jpg
TO:
73000.jpg
73006.jpg
DO:
use this PascalScript with den4b ReNamer:

(see our wiki for an howto using rules and scripts ReNamer:Rules:PascalScript)

var
SubPatterns: TStringsArray;

Begin
   SubPatterns:=SubMatchesRegEx(WideExtractBaseName(FileName),'(.*?)(\d+)(.*)',false);
   if Length(SubPatterns) <=0 then exit;

   FileName := SubPatterns[0] + IntToStr( StrToInt( SubPatterns[1] ) + 61000 ) 
                  + SubPatterns[2] + WideExtractFileExt(FileName);
End.

Once more as step-by-step:

var
SubPatterns: TStringsArray;
NumberFound, Calculation: integer;

Begin

  //split the base name into parts to find an number (maybe you have to adjust the regex):
  SubPatterns:=SubMatchesRegEx(WideExtractBaseName(FileName),'(.*?)(\d+)(.*)',false);
  if Length(SubPatterns) <=0 then exit;

  NumberFound := StrToInt( SubPatterns[1] ); 

   //do your calculation here, like '+ 100' or '- 25':
   //Note that here is no error handling involved... take care yourself if the math will make sense!
  Calculation := NumberFound + 61000 ;

  FileName := SubPatterns[0] + IntToStr(Calculation) + SubPatterns[2]  
                 + WideExtractFileExt(FileName);
End.


  HTH? big_smile
If yes, see my signature if you are able to help Denis, thanks.


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 2011-12-07 04:46

Lukers18
Member
Registered: 2011-12-07
Posts: 4

Re: Calculate and increase number found in filename

Hello, Stefan.

I have some files that have 2 numeric sequences in name (e.g. abc 001-002) and want to calculate and increment both sequences. Is it possible?

Can you help me? Tried to modify your script but had no success... =/

Offline

#4 2011-12-07 08:10

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

Re: Calculate and increase number found in filename

Lukers18 wrote:

Hello, Stefan.

I have some files that have 2 numeric sequences in name (e.g. abc 001-002) and want to calculate and increment both sequences. Is it possible?

Can you help me? Tried to modify your script but had no success... =/

Yes it's possible.

Just modify the RegEx to
'(.*?)(\d+)-(\d+)(.*)'
and then work with 'SubPatterns[1]' and 'SubPatterns[2]'

NumberFound1 := StrToInt( SubPatterns[1] );
NumberFound2 := StrToInt( SubPatterns[2] );


Later more.

In the meantime you could provide a few real, full file names.
They must have all an common pattern to detect the two numbers in question.
And what is the math to do? Increment both by one?


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

#5 2011-12-07 17:25

Lukers18
Member
Registered: 2011-12-07
Posts: 4

Re: Calculate and increase number found in filename

Hello, Stefan.

Thanks to you my problem was solved. Now I'll spend much less time renaming my comics and spend much more reading them.

You're the man, man!

Offline

#6 2013-05-13 19:56

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

Re: Calculate and increase number found in filename

Q: So, can I make calculations (addition, multiplication, etc) considering a file's name?

  For example, the file is:
   Aircraft [200 by 10].txt

   I want the result as:
   20 ~ Aircraft [200 by 10].txt

   Is this possible?


A: Yes, use this PascalScript.

( See our wiki for a how-to use PascalScript > http://www.den4b.com/wiki/ReNamer:Rules:PascalScript )

FROM:
Aircraft [200 by 10].txt
Aircraft [200 by 4].txt
Aircraft [200 by 8].txt

TO:
20 ~ Aircraft [200 by 10].txt
50 ~ Aircraft [200 by 4].txt
25 ~ Aircraft [200 by 8].txt

USE:

{
  Do the math calculation multiplication (NOTE: crassly rounded method here used)
  FROM:
    Aircraft [200 by 4].txt
  TO:
    50 ~ Aircraft [200 by 4].txt
}

var
  Base, math    : WideString;
  SubPatterns   : TStringsArray;
  nmb1, nmb2    : Integer;

Begin
   Base        := WideExtractBaseName(FileName);
   SubPatterns := SubMatchesRegEx(Base,'(.*)\[(\d+) by (\d+)\]',false);
   if Length(SubPatterns) <=0 then exit;
   
   nmb1        := StrToInt(SubPatterns[1]);
   nmb2        := StrToInt(SubPatterns[2]);
   math        := IntToStr(nmb1 / nmb2);
   FileName    := math + ' ~ ' + Base + WideExtractFileExt(FileName);
End.



Notes:
For other file name examples we have to adjust the used regex for the SubMatchesRegEx() function to get the wanted SubPatterns.
For multiplication we have to exchange '/' by '*'


.


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

#7 2013-05-21 10:33

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

Re: Calculate and increase number found in filename

-

Hmm, sure enough, after providing an simple example name only, the user
now provide more details with real examples. Well, ahead to an second round:



FROM:
Aircraft [696 by 52].txt
Aircraft [233 by 5].txt
Aircraft [245 by 8].txt

TO:
13,38  ~  Aircraft [696 by 52].txt
46,60  ~  Aircraft [233 by 5].txt
30,63  ~  Aircraft [245 by 8].txt

Instead of:
13 ~ Aircraft [696 by 52].txt
46 ~ Aircraft [233 by 5].txt
30 ~ Aircraft [245 by 8].txt

USE e.g.:

var
  SubPatterns: TStringsArray;

Begin
   SubPatterns:=SubMatchesRegEx(WideExtractBaseName(FileName),'(.*?)\[(\d+) by (\d+)\]',false);
   if Length(SubPatterns) <=0 then exit;

   FileName := FormatFloat('##00.00',  StrToFloat(SubPatterns[1]) / StrToFloat(SubPatterns[2])) 
            + '  ~  ' + WideExtractBaseName(FileName) + WideExtractFileExt(FileName);
end.

FormatFloat('##00.00',
0 means: if the output has a digit at that place, then display that digit, else display an zero 0. Means: anyway, display something at that place.
# means: if the output has a digit at that place, then display that digit, else nothing. Means: if there is no output for this place, display nothing there.

Interesting is that the format has to be international format, hence the dot as decimal delimiter,... still resulting in a comma with my german locale.

See our fine wiki for FormatFloat() parameters:
"Converts supplied floating point value to its string representation, using user specific Format. Format string may contain following specifiers: "
>>> http://www.den4b.com/wiki/ReNamer:Pasca … :Functions


More info if you google for FormatFloat, you will find e.g. delphibasics, freepascal or eulanda

.


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 2013-05-22 11:52

regularexpress
Member
Registered: 2013-05-22
Posts: 3

Re: Calculate and increase number found in filename

haha... that was faster than i thought. I see it now Stefan.

var
  SubPatterns: TStringsArray;

Begin
   SubPatterns:=SubMatchesRegEx(WideExtractBaseName(FileName),'(.*?)\[(\d+) by (\d+)\]',false);
   if Length(SubPatterns) <=0 then exit;

   FileName := FormatFloat('##0.00',  StrToFloat(SubPatterns[1]) / StrToFloat(SubPatterns[2])) 
            + '  ~  ' + WideExtractBaseName(FileName) + WideExtractFileExt(FileName);
end.

FROM:
Aircraft [696 by 52.0833333].txt

TO:
13,36  ~  Aircraft [696 by 52.0833333].txt

The number of decimals in the both numbers of the original name can vary.

From my ignorant view, if it's even possible, I'd have to change the 'd' in the "(\d+)" of the code?

Ok I know I'm becoming more and more abusive, but my learning curve for Pascal Script is really slow lol. Really thank you for all the answers. I guess I was innocent to believe that the problems couldn't get more complex.

ps: Also I wondered if I can change a number in a file from seconds to minutes and seconds, or hours,minutes and seconds, etc. From "70s ~ Aircraft [140 by 2]" to "1min10s ~ Aircraft [140 by 2]", for example.

Last edited by regularexpress (2013-05-22 11:52)

Offline

#9 2013-05-22 13:42

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

Re: Calculate and increase number found in filename

Hi and welcome.

regularexpress wrote:

FROM:
Aircraft [696 by 52.0833333].txt

TO:
13,36  ~  Aircraft [696 by 52.0833333].txt

The number of decimals in the both numbers of the original name can vary.

From my ignorant view, if it's even possible, I'd have to change the 'd' in the "(\d+)" of the code?

Ok I know I'm becoming more and more abusive, but my learning curve for Pascal Script is really slow lol.

1) this '\d' is not PascalScript but a Regular Expression.
2) with '\d+' we simply try to match one-or-more of any digit out of '0123456789'
3) to match '52' OR '52.08' we have to change this expression.
4) the simplest way is to use an dot to match any kind of signs.
  ' .+'  means: match one-or-more of any sign

So just try '(.*?)\[(.+) by (.+)\]'
Since we have the anchors '[' and ' by ' and ']', I guess we will always match the right thing?


- - -

ps: Also I wondered if I
can change a number in a file from seconds to minutes and seconds,
or hours,minutes and seconds, etc.

From
"70s ~ Aircraft [140 by 2]"
to
"1min10s ~ Aircraft [140 by 2]", for example.

A, you mean:
70 / 60 = 1m + rest 10s
133 / 60 = 2m + rest 15s

I don't know at the moment. But seams like to be a simple math?
What would be the pattern to match for the math? Always the leading number followed by an 's'?


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

#10 2013-05-22 18:52

regularexpress
Member
Registered: 2013-05-22
Posts: 3

Re: Calculate and increase number found in filename

Stefan wrote:

1) this '\d' is not PascalScript but a Regular Expression.
2) with '\d+' we simply try to match one-or-more of any digit out of '0123456789'
3) to match '52' OR '52.08' we have to change this expression.
4) the simplest way is to use an dot to match any kind of signs.
  ' .+'  means: match one-or-more of any sign

So just try '(.*?)\[(.+) by (.+)\]'
Since we have the anchors '[' and ' by ' and ']', I guess we will always match the right thing?

Yes! Damn very explained, tyvm again.

Stefan wrote:

A, you mean:
70 / 60 = 1m + rest 10s
133 / 60 = 2m + rest 15s

I don't know at the moment. But seams like to be a simple math?
What would be the pattern to match for the math? Always the leading number followed by an 's'?

Yes, always the leading number (always integer) followed by 's'.

Hmm... I realize the questions are two (and only two for certain this time):
seconds to minutes and seconds
seconds to hours, minutes and seconds

And yea, after you mentioned "rest" even I realized it needs to play with integer division, rest, and in the hours case it needs to make an integer divison with the already obtained rest of the integer divison by 3600... then again the most difficult part is to write that, and so far the only thing that comes to my mind StrToInt for integer divison (well you used it and it's on this topic already tongue). And I couldn't even do that because I still wouldn't get past the "SubPatterns" step.

Offline

Board footer

Powered by FluxBB