#1 2022-03-29 17:10

damondeaton
Member
From: Richland, WA
Registered: 2021-10-04
Posts: 4

Please help with pascal code to rename file

I am trying to figure out the code to rename files in the following format:

Original Filenames
H-1-89101-001-08.pdf
H-1-89104-004-06.pdf
H-1-89120-001-09B.pdf
H-1-89120-012-00.pdf

New Filenames
H-1-89101-001-09(08).pdf
H-1-89104-004-07(06).pdf
H-1-89120-001-10B(09).pdf
H-1-89120-012-01(00).pdf

Step 1: Copy the two-digit number at positions 15 & 16 then paste the value at the end of the filename enclosed within (##).
Step 2: Increment the two-digit number at positions 15 & 16 by +1

Here is the code I use to increment the two-digit number at positions 15 & 16 by +1

var
  Number:string;
  NewNumber:string;
  prefix:string;
begin
  Number:=copy(Filename,15,2);
  prefix:=copy(Filename,1,14);
  NewNumber:=Inttostr(StrToInt(Number)+1);
  while (length(NewNumber)<2) do
  begin
    NewNumber:='0'+NewNumber;
  end;
  Filename:= prefix + NewNumber +  WideExtractFileExt(FileName);
end.

Damon Deaton

Offline

#2 2022-03-29 20:22

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

Re: Please help with pascal code to rename file

Well done.

You have done the whole work on your own.

Here is just a little improvement:

var
  BaseName, prefix,Number,NewNumber,AfterNumber:WideString;

begin
  BaseName := WideExtractBaseName(FileName)
  prefix:=copy(BaseName,1,14);
  Number:=copy(BaseName,15,2);
  AfterNumber:=copy(BaseName,17,1);
  NewNumber:=Inttostr(StrToInt(Number)+1);
  while (length(NewNumber)<2) do
  begin
    NewNumber:='0'+NewNumber;
  end;
  Filename:=prefix + NewNumber+AfterNumber + '('+Number+')'
   + WideExtractFileExt(FileName);
end.     


WideString: work with other than just basic ascii char set (i.e.: with Unicode too)
BaseName: exclude the extension for the copy actions, may help in some situations


Since there is only one line of code, you could drop "  begin+  end;" on that while statement

  while (length(NewNumber)<2) do
      NewNumber:='0'+NewNumber;



HTH? big_smile


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 2022-03-29 23:36

damondeaton
Member
From: Richland, WA
Registered: 2021-10-04
Posts: 4

Re: Please help with pascal code to rename file

Stefan:

Thank you very much for your assistance. Your code works perfectly. I have just recently begun the, ever so frustrating, task of learning how to use Pascal Scripting. I am not familiar with any programming languages other than a Fortran class I took in college 25 years ago, so I typically need all the help I can get. I really appreciate your quick response.

Thank you!!!  smile


Damon Deaton

Offline

Board footer

Powered by FluxBB