ReNamer:Scripts:TrID

From den4b Wiki
Revision as of 10:27, 25 May 2010 by Den4b (talk | contribs) (added link to forum discussions and navigation)
Jump to navigation Jump to search

This script integrates a famous TrID library which is used for detecting file extensions. Discussions about this script and some script variants can be found on forum: ReNamer and TrID library for detecting file extension.

Requirements

Code

Author: Denis Kozlov. Date: 16 Dec 2008. Initial version. Sets the file extension to the top matching extension. Can be easily extended. At the moment works only on ANSI filenames (non Unicode).

{ TrID Script }

// TrID DLL exported functions
function TridAnalyze: integer;
  external 'TrID_Analyze@TrIDLib.dll stdcall';
function TridSubmitFileA(szFileName: pchar): integer;
  external 'TrID_SubmitFileA@tridlib.dll stdcall';
function TridLoadDefsPack(szPath: pchar): integer;
  external 'TrID_LoadDefsPack@tridlib.dll stdcall';
function TridGetInfo(lInfoType: integer; lInfoIdx: integer; sBuf: pchar): integer;
  external 'TrID_GetInfo@tridlib.dll stdcall';

// Constants for TrID_GetInfo
const
  TRID_GET_RES_NUM         = 1;    // Get the number of results available
  TRID_GET_RES_FILETYPE    = 2;    // Filetype descriptions
  TRID_GET_RES_FILEEXT     = 3;    // Filetype extension
  TRID_GET_RES_POINTS      = 4;    // Matching points
  TRID_GET_VER             = 1001; // TrIDLib version (major * 100 + minor)
  TRID_GET_DEFSNUM         = 1004; // Number of filetypes definitions loaded

function GetTopExtensions(Max: Integer): String;
var
  S: String;
  Num, I: Integer;
begin
  Result := '';
  SetLength(S, 100);
  Num := TridGetInfo(TRID_GET_RES_NUM, 0, PChar(S));
  if Num > 0 then
  begin
    if Num > Max then
      Num := Max;
    for I:=1 to Num do
      begin
        TridGetInfo(TRID_GET_RES_FILEEXT, I, PChar(S));
        if I > 1 then Result := Result + '|';       
        Result := Result + String(PChar(S));
      end;
  end;
end;

var
  Initialized: Boolean;

begin
  if not Initialized then
  begin
    TridLoadDefsPack('');
    Initialized := True;
  end;
  TridSubmitFileA(PChar(FilePath));
  if TridAnalyze <> 0 then
    FileName := WideExtractBaseName(FileName)+'.'+GetTopExtensions(1);
end.