ReNamer:Scripts:Partial case change

From den4b Wiki
Jump to: navigation, search

Users often want to change the case of the filename and not necessarily the case of the whole name like in the Case rule. Below are several scripts which demonstrate how to change the case of specific parts of the filename. They can be easily adjusted to suit other patterns.

Tested

  • ReNamer 5.74.4 Beta

Code A

Author: Denis Kozlov. Date: 9 February 2011.

Convert to upper case everything before the first " - " (spaced dash). Beware, the filename will remain unchanged if dash is not found.

Input Name Output Name
artist name - song title ARTIST NAME - song title
no dash in the name no dash in the name
var
  I: Integer;
begin
  I := WidePos(' - ', FileName);
  if I > 0 then
    FileName := WideUpperCase(WideCopy(FileName, 1, I - 1)) +
      WideCopy(FileName, I, WideLength(FileName) - I + 1);
end.

Code B

Author: Denis Kozlov. Date: 9 February 2011.

Capitalize every word in the first part of the name separated by " - " (spaced dash) and capitalize every first letter of the remaining parts like in sentences. No limitation on number of parts. If not dashes found, the whole name is treated as a frist and only part. Leave extension unchanged.

Input Name Output Name
artist name - song title Artist Name - Song title
artist name - album name - song title Artist Name - Album name - Song title
no dash in the name No Dash In The Name
const
  DELIMITER = ' - ';

var
  NewName: WideString;
  Parts: TWideStringArray;
  I: Integer;
  
function SentenseCapitalize(const S: WideString): WideString;
begin
  if WideLength(S) > 0 then
    Result := WideUpperCase(WideCopy(S, 1, 1)) +
      WideLowerCase(WideCopy(S, 2, WideLength(S) - 1))
  else
    Result := '';
end;
  
begin
  Parts := WideSplitString(WideExtractBaseName(FileName), DELIMITER);
  if Length(Parts) > 0 then
  begin
    NewName := WideCaseCapitalize(Parts[0]);
    if Length(Parts) > 1 then
      for I := 1 to Length(Parts) - 1 do
        NewName := NewName + DELIMITER + SentenseCapitalize(Parts[I]);
    FileName := NewName + WideExtractFileExt(FileName);
  end;
end.