#1 2007-05-08 03:22

bobyang
Member
Registered: 2007-05-08
Posts: 33

rename the same filename but different extension

I just wonder if it is possible. thanks. the only thing I found is http://www.den4b.com/forum/viewtopic.php?id=155 but it is not quiet what I need. thanks!

here is something I want:
person-docname.txt -> person-docname.txt
person-docname.doc ->  person-docname(2).doc
....

what I did is
1. remove extension
2. use pascalscript to add (2)
3.... I dont' know how to add back the original extension... sad

thank you!

File?id=dc3przs8_38dsgt2dg9


could you also teach me how to write regexp correctly for unicode. thank you! I use it but it doens't return the way I but it returns correctly for non-unicode words.
thank you!!!!!!!!!!



File?id=dc3przs8_39gjr38mfh

Last edited by bobyang (2007-05-08 03:32)

Offline

#2 2007-05-08 10:27

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

Re: rename the same filename but different extension

PascalScript has a preloaded script called "Serialize Duplicates", which does almost what you wanted, except it serializes them based on the full name match (including extension). So I modified it a bit, and below is the code that will work as you wanted:

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
  NewName, Ext: WideString;
  Counter: Integer;

begin
  Counter := 2;
  Ext := WideExtractFileExt(FileName);
  NewName := WideExtractBaseName(FileName);
  while Exists(NewName) do
  begin
    NewName := 
      WideExtractBaseName(FileName) +
      ' (' + IntToStr(Counter)+')'; 
    Counter := Counter + 1;
  end;
  Add(NewName);
  FileName := NewName + Ext;
end.

NOTE: This code only checks the new name, it doesn't check the PATH. This means that 2 files with the same base name but in different folders will also get serialized! If this behavior doesn't suite you, say so, and i'll modify it again.

Offline

#3 2007-05-08 10:38

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

Re: rename the same filename but different extension

Here is the solution for your Unicode problem:

Expression: (.+?)-(.*)
Replace: $1\\$0

Offline

#4 2007-05-08 18:30

bobyang
Member
Registered: 2007-05-08
Posts: 33

Re: rename the same filename but different extension

den4b wrote:

Here is the solution for your Unicode problem:

Expression: (.+?)-(.*)
Replace: $1\\$0

thank you!!! this works wonderfully! I thought "W" was for unicode. I try to learn regexp but keep on forget all those key words sad

Offline

#5 2007-05-08 18:32

bobyang
Member
Registered: 2007-05-08
Posts: 33

Re: rename the same filename but different extension

den4b wrote:

NOTE: This code only checks the new name, it doesn't check the PATH. This means that 2 files with the same base name but in different folders will also get serialized! If this behavior doesn't suite you, say so, and i'll modify it again.

This code is great! however, I hope filename will not change if they are in the different folders. could you help me with that. thank you! and sorry take your time for that.

thank you! thank you!

Offline

#6 2007-05-08 20:16

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

Re: rename the same filename but different extension

Here is the code that will handle files from different directories (test it 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
  NewName, Path, Name, Ext: WideString;
  Counter: Integer;

begin
  Counter := 2;
  Ext := WideExtractFileExt(FileName);
  Name := WideExtractBaseName(FileName);
  Path := WideExtractFilePath(FilePath);  
  NewName := Name;
  while Exists(Path + NewName) do
  begin
    NewName := Name +' (' + IntToStr(Counter)+')'; 
    Counter := Counter + 1;
  end;
  Add(Path + NewName);
  FileName := NewName + Ext;
end.

Offline

#7 2007-05-08 20:19

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

Re: rename the same filename but different extension

bobyang wrote:

I thought "W" was for unicode. I try to learn regexp but keep on forget all those key words sad

I thought so as well, but as we both see - we were mistaken wink
Perhaps, "\w" stand for only english alpha-numeric...

Offline

#8 2007-05-08 21:27

bobyang
Member
Registered: 2007-05-08
Posts: 33

Re: rename the same filename but different extension

woderful! every thing works very good! and you are fast! I have added them into the script foder for the future usage!
thank you! thank you! thank you!

two more questions.
1. is it possible to detect the files are there already and rename it by adding (2), (3)....?

File?id=dc3przs8_42d5wz4kfk




2. I like to check if the rename has "&" in the filename. if so, don't move it to a new folder. thank you!
hope something like below.
solved:
this is waht I do
1. add reg exgeX

(.+?)&(.+?)-(.*)    to    $1&$2___$3

2. add

(.*?)-(.*)   to    $1\\$0

3. add "replace" from "___" to "-"

File?id=dc3przs8_40cq75prhr

Last edited by bobyang (2007-05-10 00:24)

Offline

#9 2007-05-10 10:17

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

Re: rename the same filename but different extension

Glad that you've solved one of your problems! smile

Other problem: Yes, possible! In fact, ReNamer already detects it, and notifies you of that.

1) Add all of your files/folder to renamer;
2) Set the rules and press Preview (and possibly Rename);
3) Files/folders that are duplicated or already exist will be marked with a warning/failed sign;
4) Right-click on the table "Clearing" -> "Clear Valid" or "Clear Successful" (if you Renamed)
5) Now you are left only with the duplicated files/folders;
6) Use SERIALIZE rule to add number sequences in any format you want!!

That will work 100% sure without any scripts!

Offline

#10 2007-05-11 03:49

bobyang
Member
Registered: 2007-05-08
Posts: 33

Re: rename the same filename but different extension

den4b wrote:

Glad that you've solved one of your problems! smile

Other problem: Yes, possible! In fact, ReNamer already detects it, and notifies you of that.

1) Add all of your files/folder to renamer;
2) Set the rules and press Preview (and possibly Rename);
3) Files/folders that are duplicated or already exist will be marked with a warning/failed sign;
4) Right-click on the table "Clearing" -> "Clear Valid" or "Clear Successful" (if you Renamed)
5) Now you are left only with the duplicated files/folders;
6) Use SERIALIZE rule to add number sequences in any format you want!!

That will work 100% sure without any scripts!

thank you! however, is there any script I can use for this? I tried to publish it after it works and have all other people use it.
a group of peopel and I have hard time to clean up the filename and all those process for a karaoke system which required us to do all those. I am thinking if i can create something by using your software with pre-set configiration, then I can give it to all people by just click on "preview" and "rename" to have everything done and make those people easy.
thank you!

http://www.youtube.com/watch?v=dNMgrl8TfG8

Offline

Board footer

Powered by FluxBB