Difference between revisions of "ReNamer:Scripts:Serialize duplicates"

From den4b Wiki
Jump to navigation Jump to search
(Navigation)
(Replaced the use of TStringsArray with TWideStringArray.)
Line 35: Line 35:
 
<source>
 
<source>
 
var
 
var
   Files: TStringsArray;
+
   Files: TWideStringArray;
  
 
procedure Add(const S: WideString);
 
procedure Add(const S: WideString);
Line 99: Line 99:
 
<source>
 
<source>
 
var
 
var
   Files: TStringsArray;
+
   Files: TWideStringArray;
  
 
procedure Add(const S: WideString);
 
procedure Add(const S: WideString);

Revision as of 11:32, 5 February 2017

Script will serialize duplicated names by appending a counter to the end of the base name. Note that only the file name is taken into consideration, so files with the same name but in different folders will still be serialized as if they were in the same folder.

Tested

  • ReNamer 5.50

Code 1

Author: Denis Kozlov. Date: 26 January 2007.

Example Output
File A.txt File A.txt
File A.txt File A (2).txt
File A.txt File A (3).txt
File B.doc File B.doc
File C.mp3 File C.mp3
File B.doc File B (2).doc
var
  Files: TWideStringArray;

procedure Add(const S: WideString);
begin
  SetLength(Files, Length(Files)+1);
  Files[Length(Files)-1] := S;
end;

function Exists(const S: WideString): Boolean;
var I: Integer;
begin
  Result := False;
  for I:=0 to Length(Files)-1 do
    if WideSameText(Files[I], S) then
      begin Result := True; Break; end;
end;

var
  NewFileName: WideString;
  Counter: Integer;

begin
  Counter := 2;
  NewFileName := FileName;
  while Exists(NewFileName) do
  begin
    NewFileName := WideExtractBaseName(FileName) +
      ' (' + IntToStr(Counter)+')' + WideExtractFileExt(FileName);
    Counter := Counter + 1;
  end;
  FileName := NewFileName;
  Add(FileName);
end.

Code 2

Author: Denis Kozlov. Date: 22 May 2010.

Example Output
File A.txt File A -1.txt
File A.txt File A -2.txt
File A.txt File A -3.txt
File B.doc File B -1.doc
File C.mp3 File C -1.mp3
File B.doc File B -2.doc
var
  Files: TWideStringArray;

procedure Add(const S: WideString);
begin
  SetLength(Files, Length(Files)+1);
  Files[Length(Files)-1] := S;
end;

function Exists(const S: WideString): Boolean;
var I: Integer;
begin
  Result := False;
  for I:=0 to Length(Files)-1 do
    if WideSameText(Files[I], S) then
      begin Result := True; Break; end;
end;

function InsertCounter(const S: WideString; I: Integer): WideString;
begin
  Result := WideExtractBaseName(S) + ' -' +
    IntToStr(I) + WideExtractFileExt(S);
end;

var
  NewFileName: WideString;
  Counter: Integer;

begin
  Counter := 1;
  NewFileName := InsertCounter(FileName, Counter);
  while Exists(NewFileName) do
  begin
    NewFileName := InsertCounter(FileName, Counter);
    Counter := Counter + 1;
  end;
  FileName := NewFileName;
  Add(FileName);
end.