ReNamer:Scripts:EAN-13

From den4b Wiki
Revision as of 15:00, 8 February 2017 by Den4b (talk | contribs) (Text replacement - "</source>" to "</syntaxhighlight>")
Jump to navigation Jump to search

This script will calculate the checksum digit for the EAN-13 barcode. The checksum digit will be appended to the end of the base name.

Although EAN-13 codes are 13 digits long (12 + check digit) the script will work for codes of any length.

Input Output
002010000000.png 0020100000007.png
002010000001.png 0020100000014.png
002010000002.png 0020100000021.png
002010000003.png 0020100000038.png
007567816412.png 0075678164125.png

Tested

  • ReNamer 5.50+ Beta

Code

Author: Denis Kozlov. Date: 9 June 2010.

<source> function EAN13Checksum(const EAN13: String): Integer; var

 I, Digit: Integer;
 Odd: Boolean;
 Sum: Integer;

begin

 Sum := 0;
 Odd := True;
 for I := Length(EAN13) downto 1 do
 begin
   Digit := StrToIntDef(EAN13[I], 0);
   if Odd then
     Sum := Sum + Digit * 3
   else
     Sum := Sum + Digit;
  Odd := not Odd;
 end;
 Result := Sum mod 10;
 if Result <> 0 then
   Result := 10 - Result;

end;

var

 BaseName, Ext: WideString;
 Checksum: Integer;

begin

 BaseName := WideExtractBaseName(FileName);
 Ext := WideExtractFileExt(FileName);
 Checksum := EAN13Checksum(BaseName);
 FileName := BaseName + IntToStr(Checksum) + Ext;

end. </syntaxhighlight>