Difference between revisions of "ReNamer:Scripts:Serialize duplicates"
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…') |
(→Code 1) |
||
Line 58: | Line 58: | ||
NewFileName := FileName; | NewFileName := FileName; | ||
while Exists(NewFileName) do | while Exists(NewFileName) do | ||
− | + | begin | |
− | NewFileName := | + | NewFileName := WideExtractBaseName(FileName) + |
− | + | ' (' + IntToStr(Counter)+')' + WideExtractFileExt(FileName); | |
− | ' (' + IntToStr(Counter)+')' + | ||
− | |||
Counter := Counter + 1; | Counter := Counter + 1; | ||
− | + | end; | |
FileName := NewFileName; | FileName := NewFileName; | ||
Add(FileName); | Add(FileName); |
Revision as of 22:24, 22 May 2010
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: TStringsArray;
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: TStringsArray;
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.