#11 2013-05-22 20:42

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

Re: Calculate and increase number found in filename

Basically it's doable.



##########################################

Proof Of Concept

FROM:
15899
60556
60
67
865
75
125
87
55
33
7
0

TO:
15899 secs = 04h24m59s
60556 secs = 16h49m16s
60 secs = 60s
67 secs = 01m07s
865 secs = 14m25s
75 secs = 01m15s
125 secs = 02m05s
87 secs = 01m27s
55 secs = 55s
33 secs = 33s
7 secs = 07s
0 secs = 00s

Seams to work. Please check if the calculation is correct.

Now only the part to extract the leading number of the filename and work on them instead have to be coded....

But I guess there must be a shorter and more elegant algorithm to do the math:

var
    iSec, iMin, iHour      : Integer;
    sSec, sMin, sHour, Msg : String;
 
begin
  sSec  := FileName;
  iSec  := StrToInt(sSec);
  iMin  := 0;
  iHour := 0;
  Msg   := sSec + ' secs = ';
        
  if ( iSec > 60 ) then
     begin
        while ( iSec > 60) do
          begin
             iMin := iMin + 1
             iSec := iSec -60
          end
     end;

  if ( iMin > 60 ) then
     begin
        while ( iMin > 60) do
          begin
              iHour := iHour + 1
              iMin  := iMin -60
         end
     end;

   if iHour > 0 then 
      begin
          sHour := IntToStr(iHour)
          if length(sHour) < 2 then sHour := '0'+sHour;
          Msg := Msg + sHour + 'h'
      end;

  if iMin > 0 then 
     begin
          sMin := IntToStr(iMin)
          if length(sMin) < 2 then sMin := '0'+sMin;
          Msg := Msg + sMin + 'm'
     end;
  
  if iSec > 0 then
     begin
           sSec := IntToStr(iSec)
           if length(sSec) < 2 then sSec := '0'+sSec;
           Msg := Msg + sSec + 's'
     end
  else
      Msg := Msg + '00s';
      
   //compose new filename:
   FileName := Msg ;
end.

##########################################


So, next free hour, so here is it...


Working ready to use script for this example file names

FROM
15899s ~ myFileNames.ext
60556s ~ myFileNames.ext
60s ~ myFileNames.ext
67s ~ myFileName.ext
865s ~ myFileName.ext
75s ~ myFileName.ext
125s ~ myFileName.ext
87s ~ myFileName.ext
55s ~ myFileName.ext
33s ~ myFileName.ext
7s ~ myFileName.ext
0s ~ myFileName.ext

TO
04h24m59s ~ myFileNames.ext
16h49m16s ~ myFileNames.ext
00h00m60s ~ myFileNames.ext
00h01m07s ~ myFileName.ext
00h14m25s ~ myFileName.ext
00h01m15s ~ myFileName.ext
00h02m05s ~ myFileName.ext
00h01m27s ~ myFileName.ext
00h00m55s ~ myFileName.ext
00h00m33s ~ myFileName.ext
00h00m07s ~ myFileName.ext
00h00m00s ~ myFileName.ext

USE PascalScript:

//FROM:     15899s ~ myFileNames.ext
//TO:    04h24m59s ~ myFileNames.ext
//DO: search first 's' and get the number beforehand, then
//break that seconds up into hours, minutes and rest seconds.

var
 iPos, iSec, iMin, iHour   :Integer;
 sSec, sMin, sHour, sOut   :WideString;
 
begin

  iPos  := WidePos( 's' , FileName );
  if iPos <= 0 then Exit; //means: skip this file
  
  sSec  := WideCopy(FileName, 0, iPos -1);
  iSec  := StrToInt(sSec);

  iMin  :=  0;
  iHour :=  0;
  sOut  := '';
        
  if ( iSec > 60 ) then
    begin
        while ( iSec > 60) do
        begin
          iMin := iMin + 1;
          iSec := iSec -60;
        end
    end;

  if ( iMin > 60 ) then
    begin
      while ( iMin > 60) do
      begin
        iHour := iHour + 1;
        iMin  := iMin -60;
      end
    end;

   if iHour > 0 then 
   begin
      sHour := IntToStr(iHour)
      if length(sHour) < 2 then sHour := '0'+sHour;
      sOut := sOut + sHour + 'h';
   end
   else
      sOut := sOut + '00h';

  if iMin > 0 then 
  begin
      sMin := IntToStr(iMin)
      if length(sMin) < 2 then sMin := '0'+sMin;
      sOut := sOut + sMin + 'm';
  end
  else
      sOut := sOut + '00m';
  
  if iSec > 0 then
  begin
      sSec := IntToStr(iSec)
      if length(sSec) < 2 then sSec := '0'+sSec;
      sOut := sOut + sSec + 's';
  end
  else
      sOut := sOut + '00s';
      
   //compose new file name:
   //collected sOut plus rest from origin name, from found 's'-pos up till the end//
   FileName := sOut + WideCopy(FileName, iPos +1, 999);
end.

===============================

Note:

If you don't like double zero'ed  hours and minutes,
04h24m59s ~ myFileNames.ext
00h14m25s ~ myFileName.ext
00h00m07s ~ myFileName.ext

but rather an output like:

04h24m59s ~ myFileNames.ext
14m25s ~ myFileName.ext
07s ~ myFileName.ext


then just replace

  end
  else
      sOut := sOut + '00h';
  end
  else
      sOut := sOut + '00m';

by

  end;

##########################################


big_smile big_smile big_smile If that helps somebody, then please donate to Denis. Or help four others too yourself in the next 30 days big_smile big_smile big_smile

.

Last edited by Stefan (2013-05-23 19:03)


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

#12 2013-05-23 13:29

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

Re: Calculate and increase number found in filename

Thanks for the advices. Yea although you made it look simple in the start, indeed solutions don't fall from the sky and repeating "thank you" isn't the best way to show appreciation. I think the FROM/TO you gave is pretty good, now the scheme you gave it's gonna take me some time to give an opinion but it looks good enough too.

Offline

#13 2013-05-23 19:11

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

Re: Calculate and increase number found in filename

-


Got a free hour, so back to regularexpress...  I have updated my post above and provided a working script. Enjoy!






.


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

Board footer

Powered by FluxBB