ReNamer:Scripts:Serialize duplicates
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 |
<syntaxhighlight lang="pascal"> 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. </source>
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 |
<syntaxhighlight lang="pascal"> 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. </source>