#1 2020-02-12 21:22

Dirtylobster
Member
Registered: 2020-02-12
Posts: 1

Rename to max file name length according to pattern

I have a number of files named as pattern RELEASENAME - PERFORMER - TITLE - GENRE.EXTENSION that I'm moving to a NAS that only supports a file name length of 143 characters including the extension.
I would like to rename the files so that the only the TITLE part of the current filename is truncated but all other parts of the filename are retained.

I would very much appreciate help on how to automate this as it would save me from having to manually rename several thousand files.

Offline

#2 2020-02-12 23:57

den4b
Administrator
From: den4b.com
Registered: 2006-04-06
Posts: 2,906

Re: Rename to max file name length according to pattern

This can be achieved with the Pascal Script rule.

The script below will shrink the 3rd part in the filename delimited by " - ", such that the whole filename length does not exceed MaxFileNameLength. Other parts will not be affected, so it is possible for the filename to still exceed the maximum length if other parts alone exceed it. If a filename does not match the pattern, it will be skipped.

const
  MaxFileNameLength = 143;
var
  Parts: TWideStringArray;
  ShrinkBy: Integer;
begin
  if Length(FileName) > MaxFileNameLength then
  begin
    Parts := SubMatchesRegEx(FileName, '\A(.+?) - (.+?) - (.+?) - (.+?)\.([^\.]+)\Z', False);
    if Length(Parts) = 5 then
    begin
      ShrinkBy := Length(FileName) - MaxFileNameLength;
      Parts[2] := WideCopy(Parts[2], 1, Length(Parts[2]) - ShrinkBy);
      FileName := Parts[0] + ' - ' + Parts[1] + ' - ' + Parts[2] + ' - ' + Parts[3] + '.' + Parts[4];
    end;
  end;
end.

Offline

Board footer

Powered by FluxBB