Difference between revisions of "ReNamer:Scripts:EAN-13"

From den4b Wiki
Jump to navigation Jump to search
(added another example)
m (Text replacement - "<source>" to "<syntaxhighlight lang="pascal">")
 
(2 intermediate revisions by the same user not shown)
Line 2: Line 2:
  
 
This script will calculate the checksum digit for the [http://en.wikipedia.org/wiki/EAN-13 EAN-13 barcode]. The checksum digit will be appended to the end of the base name.
 
This script will calculate the checksum digit for the [http://en.wikipedia.org/wiki/EAN-13 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.
  
 
{| class="wikitable"
 
{| class="wikitable"
Line 31: Line 33:
 
Author: Denis Kozlov. Date: 9 June 2010.
 
Author: Denis Kozlov. Date: 9 June 2010.
  
<source>
+
<syntaxhighlight lang="pascal">
 
function EAN13Checksum(const EAN13: String): Integer;
 
function EAN13Checksum(const EAN13: String): Integer;
 
var
 
var
Line 64: Line 66:
 
   FileName := BaseName + IntToStr(Checksum) + Ext;
 
   FileName := BaseName + IntToStr(Checksum) + Ext;
 
end.
 
end.
</source>
+
</syntaxhighlight>

Latest revision as of 16:02, 8 February 2017

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.