Difference between revisions of "ReNamer:Scripts:TrID"

From den4b Wiki
Jump to navigation Jump to search
(→‎Code: source tag)
(added link to forum discussions and navigation)
Line 1: Line 1:
This script integrates a famous [http://mark0.net/code-tridlib-e.html TrID library] which is used for detecting file extensions.
+
{{Up|ReNamer:Scripts}}
 +
 
 +
This script integrates a famous [http://mark0.net/code-tridlib-e.html TrID library] which is used for detecting file extensions. Discussions about this script and some script variants can be found on forum: [http://www.den4b.com/forum/viewtopic.php?id=550 ReNamer and TrID library for detecting file extension].
  
 
== Requirements ==
 
== Requirements ==

Revision as of 11:27, 25 May 2010

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.