Difference between revisions of "ReNamer:Scripts:Serialize duplicates"
Jump to navigation
Jump to search
(Navigation) |
m (Text replacement - "</source>" to "</syntaxhighlight>") |
||
(3 intermediate revisions by the same user not shown) | |||
Line 4: | Line 4: | ||
== Tested == | == Tested == | ||
− | * ReNamer 5. | + | * ReNamer 5.74.4 Beta |
== Code 1 == | == Code 1 == | ||
Line 33: | Line 33: | ||
|} | |} | ||
− | < | + | <syntaxhighlight lang="pascal"> |
var | var | ||
− | Files: | + | Files: TWideStringArray; |
procedure Add(const S: WideString); | procedure Add(const S: WideString); | ||
Line 68: | Line 68: | ||
Add(FileName); | Add(FileName); | ||
end. | end. | ||
− | </ | + | </syntaxhighlight> |
== Code 2 == | == Code 2 == | ||
Line 97: | Line 97: | ||
|} | |} | ||
− | < | + | <syntaxhighlight lang="pascal"> |
var | var | ||
− | Files: | + | Files: TWideStringArray; |
procedure Add(const S: WideString); | procedure Add(const S: WideString); | ||
Line 137: | Line 137: | ||
Add(FileName); | Add(FileName); | ||
end. | end. | ||
− | </ | + | </syntaxhighlight> |
Latest revision as of 15: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.