#1 2008-10-20 12:11

Pali
Member
Registered: 2008-10-20
Posts: 4

Serialize file name starting from the previous highest number

First of all , a very neat program.  i have just started using it and it is very helpful.

I have a scenario which  i am requesting help with please....

I have a folder which i wish to serialize (file names).  That is ok.
I then add more files to this folder.  I now want to serialize the  new files but i want the index to begin from where it finished previously.

EG

I have test001, test002

I add new files to the folder.  I now wish to rename these so they continue the numbering sequence...test003, test004 etc..


I am sure this had probably been explained in this forum, but i couldnt find the exact solution.

Any hints would be much appreciated.......


Pali

Offline

#2 2008-10-20 17:16

SafetyCar
Senior Member
Registered: 2008-04-28
Posts: 446
Website

Re: Serialize file name starting from the previous highest number

It can't start automatically from the last. So you will have to look for the last number and after that, with the serialize rule in "index starts" put the next number

I don't know if you where asking just about this or need more help...


If this software has helped you, consider getting your pro version. :)

Offline

#3 2008-10-20 19:34

Pali
Member
Registered: 2008-10-20
Posts: 4

Re: Serialize file name starting from the previous highest number

thanks


Any hint as to how i can look for the last highest number.

i would know how to do it in Vbscript but i have no experience in delphi....




thanks again

Offline

#4 2008-10-20 21:57

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

Re: Serialize file name starting from the previous highest number

Hi Pali and welcome to the community. big_smile

Pali wrote:

Any hint as to how i can look for the last highest number.

Just scroll down and take an look yourself? Perhaps before adding more files.

Kind of automation:
_  add rule to delete last chars (the numbers) See "Delete"-rule and right-to-left option
_ and then just serialize all again, older and newer files together.


HTH?


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 2008-10-21 09:01

Pali
Member
Registered: 2008-10-20
Posts: 4

Re: Serialize file name starting from the previous highest number

Yes, i could just check the previous highest number and manually change the start number for the serialization rule.

But i want to be able to run this silently, via a command line, without manual intervention.  It is for a user that is totally non-computer literate.

Also, i dont want previously serialized files to change names.  If i re-serialize everytime, then filenames will change.  The previous file names must remain because they will be used.

I was hoping there would be a way of doing it via a script.  EG
1. Read all the file names in the folder and get the last 4 charahcters (the serialized numbers)
2. Put them into an array
3. choose the highest number in the array.
4. increment the highest number by 1 and put in a variable.
5. tell the serilaize rule to start from the variable set above.


Thanks for the responses so far......

Offline

#6 2008-10-23 17:27

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

Re: Serialize file name starting from the previous highest number

Pali wrote:

1. Read all the file names in the folder and get the last 4 charahcters (the serialized numbers)
2. Put them into an array
3. choose the highest number in the array.
4. increment the highest number by 1 and put in a variable.
5. tell the serilaize rule to start from the variable set above.

It is doable, just requires some coding... The only problem is the last point "tell the serialize rule to start from...", because scripts have no connection to other rules. You will just have to do serialization with the same script, like shown below.

function ExtractNumber(S: String; out Number: Integer): Boolean;
var
  I, PosStart, PosEnd: Integer;
begin
  Result := False;
  PosStart := -1;
  Posend := -1;
  for I:=Length(S) downto 1 do
    if IsWideCharDigit(S[i]) then
      begin
        PosEnd := I;
        Break;
      end;
  if PosEnd > 0 then
  for I:=PosEnd-1 downto 1 do
    if not IsWideCharDigit(S[i]) then
      begin
        PosStart := I + 1;
        Break;
      end;
  Result := (PosStart > 0) and (PosEnd > 0);
  if Result then
    Number := StrToInt(Copy(S, PosStart, PosEnd-PosStart+1));
end;

var
  Initialized: Boolean;
  Files: TStringsArray;
  I, Number, MaxNumber: Integer;

begin
  if not Initialized then
  begin
    Initialized := True;
    WideScanDirForFiles(
      WideExtractFileDir(FilePath),
      Files, False, False, False, '*.*');
    MaxNumber := 0;
    for I:=0 to Length(Files)-1 do
      if ExtractNumber(WideExtractBaseName(Files[i]), Number) then
        if Number > MaxNumber then MaxNumber := Number;
  end;
  MaxNumber := MaxNumber + 1;
  FileName := WideExtractBaseName(FileName) +
    IntToStr(MaxNumber) + WideExtractFileExt(FileName);
end.

Here is a screenshot of how it works:

serializemaxnumbernb8.gif

Offline

#7 2008-10-26 20:46

Pali
Member
Registered: 2008-10-20
Posts: 4

Re: Serialize file name starting from the previous highest number

Cheers for that DEN4B.

I tried ur function and it worked as expected.
However I finally ended up doing it by using vbscript to edit the .rnp files with the values required.

Here is the script im using.

FileName = "C:\Program Files\ReNamer\Presets\ClearVision.rnp"



NewIndexStart = Inputbox ("Enter number you want to start with", "Rename Files - Enter Number")
If NewIndexStart = "" Then
    msgbox "You must enter a number"
    wscript.Quit()
End If


NewPrefix = Inputbox ("Enter letters you want to start with", "Rename Files - Enter Letters")
If NewPrefix = "" Then
    msgbox "You must enter correct pre-fix"
    wscript.Quit()
End If


'Read source text file
FileContents = GetFile(FileName)

'Find the exact INDEX value
indexpos = Instr(1,FileContents,"INDEX")
ColonPos = Instr(indexpos,FileContents,";")
LengthOfOldIndex = ColonPos - indexpos 

Find = Mid(FileContents,indexpos,LengthOfOldIndex)
ReplaceWith = "INDEX:" & NewIndexStart 


'replace all string In the source file
dFileContents = replace(FileContents, Find, ReplaceWith, 1, -1, 1)


'Find the exact TEXT value
Textpos = Instr(1,dFileContents,"Config=TEXT:")
ColonPos = Instr(Textpos,dFileContents,";")
LengthOfText = ColonPos - Textpos 

Find = Mid(dFileContents,Textpos,LengthOfText)
ReplaceWith = "Config=TEXT:" & NewPrefix



'replace all string In the source file
dFileContents = replace(dFileContents, Find, ReplaceWith, 1, -1, 1)


'Compare source And result
if dFileContents <> FileContents Then
  'write result If different
   WriteFile FileName, dFileContents

  Wscript.Echo "Script Updated."
  
Else
  'Wscript.Echo "Searched string Not In the source file"
End If




'run renamer.exe
Set WshShell = WScript.CreateObject("WScript.Shell")
exitcode = WshShell.run("""C:\Program Files\ReNamer\ReNamer.exe"" /rename ""ClearVision"" ""C:\Data\Clear Vision\Holland\Scanned Invoices\PART""",1,TRUE)
If exitcode = 0 Then
    msgbox "Files renamed successfully!"

Else msgbox "There was a problem with this program.  Please contact Pali"

End If




'Read text file
function GetFile(FileName)
  If FileName<>"" Then
    Dim FS, FileStream
    Set FS = CreateObject("Scripting.FileSystemObject")
      on error resume Next
      Set FileStream = FS.OpenTextFile(FileName)
      GetFile = FileStream.ReadAll
  End If
End Function

'Write string As a text file.
function WriteFile(FileName, Contents)
  Dim OutStream, FS

  on error resume Next
  Set FS = CreateObject("Scripting.FileSystemObject")
    Set OutStream = FS.OpenTextFile(FileName, 2, True)
    OutStream.Write Contents
End Function

Just another quick question please.  Does renamer.exe return any exit codes that could identify if there was a problem whilst executing from the command line?


Otherwise, a fantastic little program that i would recommend to all......

Offline

Board footer

Powered by FluxBB