ReNamer:Scripts:Index files per folder

From den4b Wiki
Jump to: navigation, search

This script adds a serialization index to the end of every file on per folder basis. The index is incremented only when the folder path changes.

Below are the parameters of the script:

  • INDEX_START - The first index to be used.
  • PAD_LENGTH - Length to which the index is padded with zeros.
  • INDEX_DELIMETER - Prefix for the index when inserting into the filename.
  • SUFFIX - If TRUE, the index is appened to the end of the filename. If FALSE, index is prefixed.

Below is the example of the results:

Folder Name New Name
C:\TEMP\Folder A\ A1.doc A1_001.doc
C:\TEMP\Folder A\ A2.doc A2_001.doc
C:\TEMP\Folder A\ A3.doc A3_001.doc
C:\TEMP\Folder B\ B1.doc B1_002.doc
C:\TEMP\Folder C\ C1.doc C1_003.doc
C:\TEMP\Folder C\ C2.doc C2_003.doc

Note: You need to sort your files by Folder column so that all files from within the same folder appear together, because the index is changed every time the folder is changed in the list!

Tested

  • ReNamer 5.50+ Beta

Code

Author: Denis Kozlov. Date: 18 August 2011.

const
  INDEX_START = 1;
  PAD_LENGTH = 3;
  INDEX_DELIMETER = '_';
  SUFFIX = TRUE;
 
var
  Index: Integer;
  Initialized: Boolean;
  Dir, LastDir: WideString;
 
function Pad(Number, NewLength: Integer): WideString;
begin
  Result := IntToStr(Number);
  while Length(Result) < NewLength do
    Result := '0' + Result; 
end;
 
begin
  Dir := WideExtractFileDir(FilePath);
  if not Initialized then
  begin
    Initialized := True;
    LastDir := Dir;
    Index := INDEX_START;
  end;
  if not WideSameText(LastDir, Dir) then
  begin
    LastDir := Dir;
    Inc(Index);
  end;
  if SUFFIX then
    FileName := WideStripExtension(FileName) +
      INDEX_DELIMETER + Pad(Index, PAD_LENGTH) +
      WideExtractFileExt(FileName)
  else
    FileName := WideExtractFilePath(FileName) +
      Pad(Index, PAD_LENGTH) + INDEX_DELIMETER +
      WideExtractFileName(FileName);
end.