You are not logged in.
Pages: 1
I'm trying to create a PascalScript in ReNamer to be able to make it automatically select the next available number for the suffix without my constant "intervention" on the rules settings (increasing or decreasing the number, after the file fail to rename for duplicity).
The Serialize function "Random" mode has a "Unique if possible" option that would be wonderful if it was available also on the "Incremental" configuration.
The "fix conflicting new names" is also great, but I would prefer it to take "the last number string" on the file name and increase it by one, instead of adding a " (n)" suffix (if I could discover the algorithm for this script and modify it to make it alter the suffix number instead that would also be great).
For example purposes:
I have a directory with 3 old images:
Photo_Test_01.jpg
Photo_Test_02.jpg
Photo_Test_03.jpg
and 3 new images to be renamed:
DSC012544.jpg
DSC012574.jpg
DSC012591.jpg
and I want ReNamer to "figure out" the numbers 01/02/03 are already taken and then start the sufix numbers at 04, to make the new files look like:
Photo_Test_04.jpg
Photo_Test_05.jpg
Photo_Test_06.jpg
Is there some way to use a "global variable" between one file and the next? Which procedure could I use to check if the name is already "taken"?
Can someone here point me out in the right direction? (if there actually is one way to do it...)
Offline
Too bad no one was able to give me any kind of solution...
(and I haven't figured out one by myself so far... - if only I could figure out what kind of commands/logic Denis used inside the "fix conflicting new names"...)
Offline
Sorry auwebio, i missed this post.
Have you take an look at the script "Serialize duplicates.pas" in the Scripts sub folder?
Maybe you can adapt that code for your issue:
while Exists(NewFileName) do
Also i know there is an common command in all programming languages
to check if an file or folder exists: "exist" or "exists"
To see how this will work with ReNamer you can check out the wiki
or download "New Manual Draft by Narayan.zip"
which contains an "Pascal Script Manual.pdf" where you can search for "exist" and find:
If (WideFileExists( ... ) ) then
With that you can code an loop to check if an name like <FileName+Counter+Ext>
already exists, and if yes increase the counter for the next loop till the file doesn't exist already.
I don't know if this is an good start... but that's how i would start to code smtg like this.
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
Sorry, I don't know what happened, I wante to reply this message when you wrote it but I don't know why I couldn't do it at that moment and I completely forgot it.
This is a code I had on my computer:
var
Files: TStringsArray;
BasePath, BaseName,FileExt: WideString;
Count: 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;
// SKIP ITSELF
If (WideSameText(FilePath, S)) then Exit;
// CHECK FILE EXISTENCE
If (not Result) then If WideFileExists(S) then Result:=True;
// CHECK FOLDER EXISTENCE
If (not Result) then If WideDirectoryExists(S) then Result:=True;
// LOOK FOR MATCHES ON PREVIOUSLY RENAMED FILES
If (not Result) then
begin
for I:=0 to Length(Files)-1 do
begin
if WideSameText(Files[i], S) then
begin
Result := True;
Break;
end;
end;
end;
end;
begin
//GET PATH
BasePath := WideExtractFilePath(FileName);
If (BasePath='') then BasePath := WideExtractFilePath(FilePath);
// GET NAME AND EXT
If WideFileExists(FilePath) then
begin
FileExt := WideExtractFileExt(FileName);
BaseName := WideStripExtension(FileName);
end
else
begin
BaseName := FileName;
FileExt := '';
end;
// COUNT START FROM:
Count := 1;
// SERIALIZE
repeat
// BUILD THE NEW PATH
FileName := BasePath + BaseName + ' (' + IntToStr(Count) + ')' + FileExt;
Count := Count + 1;
until (not Exists(FileName));
// SAVE USED PATH
Add(FileName);
// FRIENDLY OUTPUT FORMAT
FileName := WideReplaceText(FileName, WideExtractFilePath(FilePath), '');
end.
From your examples probably you want to edit after "BUILD THE NEW PATH" to remove the brackets.
Take a try and see if that's what you wanted.
If this software has helped you, consider getting your pro version. :)
Offline
Sorry auwebio, i missed this post.
Oh... There's absolutely no need to apologize at all. I'm really happy that you caught it now.
Have you take an look at the script "Serialize duplicates.pas" in the Scripts sub folder?
Maybe you can adapt that code for your issue: while Exists(NewFileName) do
I haven't noticed that (until today)... That will probably work, indeed.
...I don't know if this is an good start... but that's how i would start to code smtg like this.
I believed you gave me all that I needed to solve my problem, Stefan! My most sincere gratitude! I'll start working on that right now.
Offline
Sorry, I don't know what happened, I wanted to reply this message when you wrote it but I don't know why I couldn't do it at that moment and I completely forgot it.
This is a code I had on my computer...
From your examples probably you want to edit after "BUILD THE NEW PATH" to remove the brackets.
Take a try and see if that's what you wanted.
You guys are really fast!
Thank you so much, SafetyCar!
That's a beautiful code, indeed. I'll edit it a little (as you suggested) and use it for my purposes.
EDIT: here's my final code; although there's still some aspects of SafetyCar's code that I don't quite understand (like the Add procedure), this one is working quite well for me. (Thanks again for providing the source code to be modified).
Of course, this PascalScript is only meant to be used after the proper rules for the generic name of the files and a proper serialize rule is in place before it can "work its magic" to avoid repeated files and automatically increase the suffix number.
var Files,SPLT: TStringsArray;
BasePath, Numbr: WideString;
Count, Pad: 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;
// SKIP ITSELF
If (WideSameText(FilePath, S)) then Exit;
// CHECK FILE EXISTENCE
If (not Result) then If WideFileExists(S) then Result:=True;
// CHECK FOLDER EXISTENCE
If (not Result) then If WideDirectoryExists(S) then Result:=True;
// LOOK FOR MATCHES ON PREVIOUSLY RENAMED FILES
If (not Result) then begin
for I:=0 to Length(Files)-1 do begin
if WideSameText(Files[i], S) then begin
Result := True; Break;
end;
end;
end;
end;
begin
//GET PATH
BasePath := WideExtractFilePath(FileName);
If (BasePath='') then BasePath := WideExtractFilePath(FilePath);
// ONLY CONTINUES IF THERE IS A NUMBER IN THE FILENAME (TO BE CHECKED AND INCREASED, IF NECESSARY)
If length(MatchesRegEx(FileName, '(.+\D)(\d+)(.+)', False))>0 then begin
// SPLIT NAME AND LAST NUMBER
SPLT := SubmatchesRegEx(FileName, '(.+\D)(\d+)(.+)', False);
// SERIALIZE
Count := 1;
repeat
Numbr := IntToStr(Count);
// ADD ZERO-PADDING, IF NECESSARY
Pad := Length(SPLT[1]) - Length(Numbr);
while (Pad>0) do begin
Numbr := '0' + Numbr;
Pad := Pad - 1;
end;
// BUILD THE NEW PATH
FileName := BasePath + SPLT[0] + Numbr + SPLT[2];
Count := Count + 1;
until (not Exists(FileName));
// SAVE USED PATH
Add(FileName);
// FRIENDLY OUTPUT FORMAT
FileName := WideReplaceText(FileName, WideExtractFilePath(FilePath), '');
end;
end.
Last edited by auwebio (2011-04-16 21:55)
Offline
Ummm, I don't know why but I see that RegEx some kind of unsafe.
Be careful with numbers on extensions, and be aware that it matches numbers that can be in de middle of the file name...
And other thing, nothing important, just that in my original code there is something like
// CHECK FILE EXISTENCE
If (not Result) then If WideFileExists(S) then Result:=True;
Which was left because iI changed some things on the code, but now it has no sense, and can be replaced with:
// CHECK FILE EXISTENCE
If WideFileExists(S) then Result:=True;
If this software has helped you, consider getting your pro version. :)
Offline
Ummm, I don't know why but I see that RegEx some kind of unsafe.
Be careful with numbers on extensions, and be aware that it matches numbers that can be in de middle of the file name...
Well... Yes... ...and no, not really. That code can't really be used to increase numbers on any kind of situation, but it does work quite well for all the purposes I have in mind for it right now. I'll admit that I was pretty lazy coding it just to the point where it started working...
The situations I'll be using it usually (99.99%) involves only jpeg images, so the number in the extension won't be a problem (at least not for me - but I get the point where it may produce something like test897.m3u/test897.m4u/test897.m5u/etc and that's definitely not intended - so, for now, it is prohibited to use the script with files where there is a number in the extension. - I could eventually add that clause in the code, but I couldn't see me using any of those extensions anytime soon, so I said "naaah, I'm feelin' kind of lazy today"...
).
As for numbers in the middle of the filename, that was indeed the intention, as I do have examples where the number is not exactly at the very right border of the filename. The Only thing that was really important for me is that the expression only got the very last number appearing in the filename, so for Test123-file32e.jpg, the expression always isolates the "32" and not the "123".
And other thing, nothing important, just that in my original code there is something like...
Which was left because I changed some things on the code, but now it has no sense, and can be replaced with...
Got it. Thanks!
I concur with most of your remarks. That code above is indeed only good for some very specific situations (on any other situation, one might make a pretty big mess in his files, it he doesn't analyze the results and catch the problem before committing the change - so it is kind of unsafe, as you said, if not used properly.)
Anyhow, thanks for you comments (and, most of all, thanks for the previous source code). I might refine this code and make it safer in the future, but right now, "I'm not feelin' it"...
(and I must confess that I'm not yet "confortable" with the specific commands for these scripts - I must check the wiki and analyze the code some more...)
Offline
Pages: 1