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

From den4b Wiki
Jump to navigation Jump to search
(Created page with '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 b…')
 
m (Text replacement - "</source>" to "</syntaxhighlight>")
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
{{Up|ReNamer:Scripts}}
 +
 
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.
 
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 ==
 
== Tested ==
* ReNamer 5.50
+
* ReNamer 5.74.4 Beta
  
 
== Code 1 ==
 
== Code 1 ==
Line 31: Line 33:
 
|}
 
|}
  
<source>
+
<syntaxhighlight lang="pascal">
 
var
 
var
   Files: TStringsArray;
+
   Files: TWideStringArray;
  
 
procedure Add(const S: WideString);
 
procedure Add(const S: WideString);
Line 58: Line 60:
 
   NewFileName := FileName;
 
   NewFileName := FileName;
 
   while Exists(NewFileName) do
 
   while Exists(NewFileName) do
    begin
+
  begin
     NewFileName :=  
+
     NewFileName := WideExtractBaseName(FileName) +
      WideExtractBaseName(FileName) +
+
       ' (' + IntToStr(Counter)+')' + WideExtractFileExt(FileName);
       ' (' + IntToStr(Counter)+')' +  
 
      WideExtractFileExt(FileName);
 
 
     Counter := Counter + 1;
 
     Counter := Counter + 1;
    end;
+
  end;
 
   FileName := NewFileName;
 
   FileName := NewFileName;
 
   Add(FileName);
 
   Add(FileName);
 
end.
 
end.
</source>
+
</syntaxhighlight>
  
 
== Code 2 ==
 
== Code 2 ==
Line 97: Line 97:
 
|}
 
|}
  
<source>
+
<syntaxhighlight lang="pascal">
 
var
 
var
   Files: TStringsArray;
+
   Files: TWideStringArray;
  
 
procedure Add(const S: WideString);
 
procedure Add(const S: WideString);
Line 137: Line 137:
 
   Add(FileName);
 
   Add(FileName);
 
end.
 
end.
</source>
+
</syntaxhighlight>

Latest revision as of 16:05, 8 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.74.4 Beta

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.