ReNamer:Scripts:EAN-13

From den4b Wiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

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.

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.