#1 2010-05-22 16:36

FranCi
Member
From: Germany
Registered: 2010-05-22
Posts: 5

Serialize duplicates - alternative

Hello,

let me first say thanks for this wonderful program. It is really one of the best, if not THE best renaming Tool ever smile
Thanks Denis.

Now to my questions.  I'm trying to serialize duplicates as a sequence - for example:
test.doc > test -1.doc
test.doc > test -2.doc
test.doc > test -3.doc
exp.exp > exp -1.doc
exp.exp > exp -2.doc

I have found the serialize duplicates pascal script, and for what i'm trying to archive it is almost perfect. It just doesn't start to serialize at the first file.

test.doc > test.doc
test.doc > test (1).doc
test.doc > test (2).doc

Could the serialize duplicates script be modified so it includes the first duplicate into the serialize?
I just encountered the power of RegEx, but Pascal Scripting is still totally new to me.

Thanks a lot

FranCi

Offline

#2 2010-05-22 22:28

den4b
Administrator
From: den4b.com
Registered: 2006-04-06
Posts: 3,379

Re: Serialize duplicates - alternative

I have added a variant of the script to do it the way you have described: Wiki: Serialize duplicates.

Note that every file that doesn't contain duplicates will still have the "-1" appended because the list of files is parsed from top to bottom and there is no way script can know whether file will have duplicates at all.

Offline

#3 2010-05-22 22:38

Stefan
Moderator
From: Germany, EU
Registered: 2007-10-23
Posts: 1,161

Re: Serialize duplicates - alternative

Hi Denis,
thats why i try to use an second script after that original serialize script
to check if an file with current name and additional "(2)" exists
and then rename this first file by adding an "(1)",... but i don't get it to work:

EDIT: i modify the code and this should work as additional script after the first serializing,
but only as second run and after the files are sorted upside down, like
file (3).txt
file (2).txt
file.txt
by an click on the header of the Name column.


As always, try this with test files first.


//if an file "<current name>" plus addition " (2)" exists, rename current name by adding an " (1)".
//Note: you have to sort the "Name" column up-side down,  Z to A, first.
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, dup: WideString;


begin
// the  content of the var 'dup' has to be adjusted to match the real file name pattern:

//here we build the dup name by extracting the part of the current name till ' (2)'
  dup := ReplaceRegEx(WideExtractBaseName(FileName), '(.+)\s\(2\)','$1',False, True);
  //showmessage('#'+dup+'#');
  dup := dup + '(2)' + WideExtractFileExt(FileName);


//now search if an dup name exists like the current name + ' ( 2)' + ext:
  if Exists(dup) then
    begin
    //showmessage(dup);    

//if such an name exists, rename current name by adding an ' (1)':
    NewFileName := 
      WideExtractBaseName(FileName) + ' (1)' + WideExtractFileExt(FileName);
    end;
  FileName := NewFileName;
  Add(FileName);
end.

Last edited by Stefan (2010-05-23 08:59)


Read the  *WIKI* for HELP + MANUAL + Tips&Tricks.
If ReNamer had helped you, please *DONATE* to Denis or buy a PRO license. (Read *Lite vs Pro*)

Offline

#4 2010-05-22 23:16

SafetyCar
Senior Member
Registered: 2008-04-28
Posts: 446
Website

Re: Serialize duplicates - alternative

Is there any way to assign variables on a pascal rule and read it from other pascal rule???


If this software has helped you, consider getting your pro version. :)

Offline

#5 2010-05-22 23:16

FranCi
Member
From: Germany
Registered: 2010-05-22
Posts: 5

Re: Serialize duplicates - alternative

Thats interesting. So it seems this is not as easy to implement as i thought.
Stefan's idea sounds not bad, if it could get to work.

Thanks you guys for taking your time with this and looking into it.

Offline

#6 2010-05-23 01:04

SafetyCar
Senior Member
Registered: 2008-04-28
Posts: 446
Website

Re: Serialize duplicates - alternative

SafetyCar wrote:

Is there any way to assign variables on a pascal rule and read it from other pascal rule???

The reply is here.




I was asking this thinking on first pascal rule that makes an array of file names (or better if they are file paths). And other array related to the first one, counting the number of times that the name appears.

For example:
test.doc
test.doc
test.doc
exp.exp

We get the arrays with;
- test.doc, exp.exp
- 3, 1


So when the data is collected now with a second pascal rule, we can say
if the filename is in the list and has more than 1 occurrences then add the serialization



Just an idea...

Last edited by SafetyCar (2010-05-23 01:07)


If this software has helped you, consider getting your pro version. :)

Offline

#7 2010-05-23 07:06

Stefan
Moderator
From: Germany, EU
Registered: 2007-10-23
Posts: 1,161

Re: Serialize duplicates - alternative

I think you can't do that with one script only,
because at first there is no other duplicate file name...
only an second same name would be an duplicate.

So i imagine you have to run an first script (or other rules)
which finds one-or-more duplicates...
and only then you can see that there is an first occurrence too...
which you can rename in an second run (as my script above does (for me)).

But changes are i am not that good in logically work and someone come up with an better solution.


Read the  *WIKI* for HELP + MANUAL + Tips&Tricks.
If ReNamer had helped you, please *DONATE* to Denis or buy a PRO license. (Read *Lite vs Pro*)

Offline

#8 2010-05-23 10:54

SafetyCar
Senior Member
Registered: 2008-04-28
Posts: 446
Website

Re: Serialize duplicates - alternative

mad My approach is wrong

I don't know why I was thinking that each rule was applied to each name before continuing with the next rule  mad


If this software has helped you, consider getting your pro version. :)

Offline

#9 2010-05-23 12:55

den4b
Administrator
From: den4b.com
Registered: 2006-04-06
Posts: 3,379

Re: Serialize duplicates - alternative

You can still do this but in 2 runs.

First run: Store in a temporary file a list of names which have duplicates.
Second run: If new name is in the list of duplicates (loaded from temp file) then serialize.

Do you need a hand with it?

Offline

#10 2010-05-23 12:59

SafetyCar
Senior Member
Registered: 2008-04-28
Posts: 446
Website

Re: Serialize duplicates - alternative

I did this, with some code from me an some from den4b script:

var
  NamesDB: TStringsArray;
  NamesTXT: WideString;
  I, Count: Integer;
  Initialized, EmptyVars: Boolean;

  Files: TStringsArray;
  NewFileName: WideString;
  Counter: Integer;

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;

begin
  NamesTXT := WideGetCurrentDir + '\~temp_vars_names.txt';

  FileName :=  ReplaceRegEx(WideExtractBaseName(FileName), ' *(\(\d+\)|-\d+)$', '', False, False) + WideExtractFileExt(FileName);
  
  If not Initialized then
    begin
      If not WideFileExists(NamesTXT) then FileWriteContent(NamesTXT, '');
      If FileReadContent(NamesTXT)='' then EmptyVars:=True else EmptyVars:=False;
    end;

  If EmptyVars then
    begin
      FileAppendContent(NamesTXT, FileName + #13#10);
      FileName := ':: Please do the Preview again ::';
    end
  else
    begin
      If not Initialized then 
      begin
        NamesDB := WideSplitString(FileReadContent(NamesTXT), #13#10);    
        WideDeleteFile(NamesTXT);
      end;
      
      Count:=0
      For I:=0 to length(NamesDB)-1 do
      begin
        If WideSameText(FileName, NamesDB[i]) then Count := Count + 1;
      end;
      
      If Count>1 then
      begin
        Counter := 1;
        NewFileName := FileName;
        If not Exists(FileName) then Add(FileName);
        while Exists(NewFileName) do
          begin
          NewFileName := 
            WideExtractBaseName(FileName) +
            ' -' + IntToStr(Counter)+'' + 
            WideExtractFileExt(FileName);
          Counter := Counter + 1;
          end;
        FileName := NewFileName;
        Add(FileName);
      end;
    end;
    
  Initialized := True;
end.

My part of the code was this.

var
  NamesDB: TStringsArray;
  NamesTXT: WideString;
  I, Count: Integer;
  Initialized, EmptyVars: Boolean;

begin
  NamesTXT := WideGetCurrentDir + '\~temp_vars_names.txt';
  
  If not Initialized then
    begin
      If not WideFileExists(NamesTXT) then FileWriteContent(NamesTXT, '');
      If FileReadContent(NamesTXT)='' then EmptyVars:=True else EmptyVars:=False;
    end;

  If EmptyVars then
    begin
      FileAppendContent(NamesTXT, FileName + #13#10);
      FileName := ':: Please do the Preview again ::';
    end
  else
    begin
      If not Initialized then 
      begin
        NamesDB := WideSplitString(FileReadContent(NamesTXT), #13#10);    
        WideDeleteFile(NamesTXT);
      end;
      
      Count:=0
      For I:=0 to length(NamesDB)-1 do
      begin
        If WideSameText(FileName, NamesDB[i]) then Count := Count + 1;
      end;
      
      If Count>1 then FileName := ':THIS FILE NEEDS SERIALIZATION: ' + FileName;
    end;
    
  Initialized := True;
end.

Gives some problems if you add more files to the list when it is asking for the new preview, but beeing careful that all files passes both steps it works ok.

Last edited by SafetyCar (2010-05-23 13:00)


If this software has helped you, consider getting your pro version. :)

Offline

Board footer

Powered by FluxBB