Use the following settings:
Delimiter: " - " (without the quotes).
New pattern: $1\$2
Skip extension [x] (ticked)
See help here: http://www.den4b.com/wiki/ReNamer:Rules:Rearrange
(See example-2 in Advanced Uses section, halfway down the page, )
I've updated from your last post, fix the ReplaceRegEx arguments were wrong and add some comments.
Seems that is working
{File delete parent n grand parent folder string}
var
Path: TStringsArray;
Depth: Integer;
FileNameTest: WideString;
FileNameTestWord: WideString;
begin
if WideFileExists(FilePath) then {Process files only}
begin
Path:=WideSplitString(FilePath,'\'); {Create Array}
Depth:=Length(Path);
If (Depth>2) then {Check Parent and Grand Parent exist in Array}
begin
FileNameTest := WideReplaceStr(Path[Depth-1], Path[Depth-2], ''); {Replace Parent (If Found)}
FileNameTest := WideReplaceStr(FileNameTest, Path[Depth-3], ''); {Replace Grand-Parent (If Found)}
FileNameTestWord := ReplaceRegEx(FileNameTest, '(-_=|\s)' , '', False, False) {RegEx to clean up a testword}
If WideLength(WideExtractBaseName(FileNameTestWord)) > 0 then {Rename only if there is something left after replacing on the testword}
FileName := FileNameTest
end
end;
end.
I just now noticed your note about "Length(FileNameTestWord) > 4", where you're checking that there is a real filename before you try to rename it. This does have some problems, however.... it assumes that the extension is always 3 characters long, which isn't necessarily true.
A better way to do this test would be "WideLength(WideExtractBaseName(FileNameTestWord)) > 0". It makes the constant a 0 (which is not as strange as 4 in program code), and addresses the issue of file extension length.
]]>It's much easier with your comments.
NOW IS TESTED BUT DON'T TRUST IT...
{File delete parent n grand parent folder string}
var
Path: TStringsArray;
Depth: Integer;
FileNameTest: WideString;
FileNameTestWord: WideString;
begin
if WideFileExists(FilePath) then
begin
Path:=WideSplitString(FilePath,'\');
Depth:=Length(Path);
If (Depth>2) then
begin
FileNameTest := WideReplaceStr(Path[Depth-1], Path[Depth-2], '');
FileNameTest := WideReplaceStr(FileNameTest, Path[Depth-3], '');
{need a RegEx clean up Here to get word length}
FileNameTestWord := ReplaceRegEx(FileNameTest, '-_=' , '', False, False)
If Length(FileNameTestWord) > 4 then
begin
FileName := FileNameTest
end
end
end;
end.
Cheers, Yogui.
]]>"Pascal Script Compile:
[Line 11] Error: Unknown identifier 'FileNameTest'"
That "Try to Compile" button is your friend. It catches simple things that might get overlooked.... like in this case, a missing variable declaration. You need to add in the line "FileNameTest: WideString;" into the "var" section of the script to fix this problem.
<Fixes this error, and finds several others>
1. When giving literal strings in the code, they must be surrounded with 's. Unlike C or Java, strings cannot be surrounded with "s.
2. An if-statement must have a THEN, to separate it from the instructions that are performed conditionally.
That covers all the bugs that were in that code that kept it from compiling.... hopefully it's at least doing what you want it to.
]]>Yes I'll take note "...I'm also holding you to your statement..."
I understand the code becomes much more complex trying to handle all possible scenarios.
My manual check is sort by new name length ascending and see the shortest ones, mostly to un-mark them.
This code may be able to check that after deletions "FileNameTest" is left with at least one word (\w)
NOTE: UNTESTED CODE, Needs someone that knows pascal to review it...
var
Path: TStringsArray;
Depth: Integer;
begin
if WideFileExists(FilePath) then
begin
Path:=WideSplitString(FilePath,'\');
Depth:=Length(Path);
If (Depth>2) then
begin
FileNameTest := WideReplaceStr(Path[Depth-1], Path[Depth-2], '');
FileNameTest := WideReplaceStr(FileNameTest, Path[Depth-3], '');
{need a RegEx clean up Here to get word length}
FileNameTestWord := ReplaceRegEx(FileNameTest, "-_=" , "" , False, False)
If Length(FileNameTestWord) > 4
begin
FileName := FileNameTest
end
end
end;
end.
NOTE: UNTESTED CODE, Needs someone that knows pascal to review it...
Length(FileNameTest) > 4 is to include file extension (increase to also include a min word length.)
Cheers, Yogui
]]>I'm also holding you to your statement "If the Parent or Grand Parent folder Names Match any string in the FileName the string is deleted." Feasibly you can have it where the artist name is part of the album name... or alternatively, that the song name is identical to that of the album name. But I leave that kinda situation for you to sort out (this could also come into play as to the order the deletions are performed...). But if you at least have a script which works, you can probably tweak it to what you need.
var
Path: TStringsArray;
Depth: Integer;
begin
if WideFileExists(FilePath) then
begin
Path:=WideSplitString(FilePath,'\');
Depth:=Length(Path);
If (Depth>2) then
begin
FileName := WideReplaceStr(Path[Depth-1], Path[Depth-2], '');
FileName := WideReplaceStr(FileName, Path[Depth-3], '');
end
end;
end.
I would like to the same you done for Folders (remove the parent name if found) to files:
var
Path, SubFolders : TStringsArray;
Depth : Integer;
begin
if not WideFileExists(FilePath) then
begin
SetLength(SubFolders,0);
Path:=WideSplitString(FilePath,'\');
WideScanDirForFolders(FilePath, SubFolders, True, False, False);
Depth:=Length(Path);
If (Depth>1) and (Length(SubFolders)=0) then
FileName := ReplaceRegEx(Path[Depth-1], '^(.*?)' + Path[Depth-2] + '(.*)$', '$1$2', false, true);
end;
end.
Ussually I'll have a file like:
...\ArtistName\AlbumName\ArtistName - AlbumName - 01 TrackName.mp3
or
...\ArtistName\AlbumName\AlbumName (ArtistName) - 01 TrackName.mp3
or
...\ArtistName\AlbumName\ArtistName - 01 TrackName (AlbumName).mp3
any should rename to:
01 TrackName.mp3
As usual clean up will be required.
So basically
If the Parent or Grand Parent folder Names Match any string in the FileName the string is deleted.
I was hopeless trying to change your code.
Hope your holidays are not over yet
Cheers, Yogui.
]]>With the (.|) I was trying to get Dirt Chars at the beginning and end like in:
"_-This is=~the filename=-.txt"
Renames to:
"This is~the filename.txt"
Works but leaves a space at the beginning, should I use a different RegEx?
Mmm... I get it, what you wanted get the replace if there is a character but also if there isn't
Technically what you wanted can be done also with (.?) but even though I think it doesn't have much sense at the beginning an the end of the expression.
I would use:
Expression: [-_,=\s\(\)\{\}\[\]|]{2}
Replace: (empty or space)
The replace depends if you just want just to delete or replace them with a space
]]>The way the Roman Numeral-->Number behaves, it will attempt to parse the numeral if it can, and if it fails for some reason, the resulting number which is returned from the function will be negative. If you look at the part of the script that searches for the "Vol <Roman>" in the filename, it does a very lazy detection: it only detects a "word" consisting of the right letters. As such, nonsense like "Vol IIIIIIIIIIII" would be picked out. However, the Roman Numeral here is clearly nonsense, and the script will return a negative value (-1 in this particular example) to reflect such. For the most part, the number given reflects the specific rule from the web page I was working from (I also used -8 in one case, because there was no associated rule number...).
I also want to throw out there, that this function is not case sensitive as it stands... uppercase and lowercase will be detected equally well (this is handled by the first line of the function, "roman := WideTrim(WideUpperCase(roman));" ). But in any case, capitalizing Roman numerals like that would be a trivial edit of the PascalScript I've already posted. Change the detecting regular expression and the argument of the WideReplaceText function. As such, I don't see any particular need to post these edits.
]]>@ SafetyCar
I didn't know about the \ inside the [] Thanks for fixing my RegEx
With the (.|) I was trying to get Dirt Chars at the beginning and end like in:
"_-This is=~the filename=-.txt"
Renames to:
"This is~the filename.txt"
Works but leaves a space at the beginning, should I use a different RegEx?
@ prologician
My RegEx Skills are not enough to even try, but I'll keep the function to later.
What I like to do with it is:
1) To find the "Vol "+<Roman> and renamed to "(Vol "+<Decimal> (as you kindly showed me already)
I may also include "Volume" as well.
2) To find \s<Roman>\s (without "Vol" or "Volume" and make it UPPER case)
These 2 fixes should be useful for other users
Any help is always welcome.
Cheers, Yogui.
]]>I do wonder if those regular expressions are being formed correctly, though.... according to http://www.regular-expressions.info/posixbrackets.html, the backslash might have a different meaning.... But that was just a quick lookover. Might be good to read over the website for more info, to get better acquainted with regular expressions.
]]>On ( ) it's used for giving options, but...
On [ ] it's a normal character, inside [ ] each letter is treated separately
You can try it
Were you posted:
[-|_|,|=|\s|\(|\)|\{|\}|\[|\]][-|_|,|=|\s|\(|\)|\{|\}|\[|\]]
Would do the same as this:
[-_,=\s\(\)\{\}\[\]|][-_,=\s\(\)\{\}\[\]|]
or
[-_,=\s\(\)\{\}\[\]|]{2}
And other thing I'm wondering. What is this '(.|)' supposed to do?, I think there is something bad on it
]]>Thanks for your solution seems powerful.
As per my test, the 1st version of your Roman Numeral algorithm function worked, the 2nd returns blank names.
Also, I've searched on the forum for a clean up reg ex with no luck...
I'm learning Reg Ex... but could not do all I want.
so I'll pile an easy one
I like to get ride of All pair of chars like:
"~-" or "()" or "(-" or "_)" or "[)" or ",," or "~~"
My Reg Ex 101 points me towards
Expression: (.|)[-|_|,|=|\s|\(|\)|\{|\}|\[|\]][-|_|,|=|\s|\(|\)|\{|\}|\[|\]](.|)
Replace: $1 $2
And then for the ones that have "~" which I like to keep
Expression: (.)[~][-|_|,|=|\s|\(|\)|\{|\}|\[|\]](.)
Replace: $1~$2
Expression: (.)[-|_|,|=|\s|\(|\)|\{|\}|\[|\]][~](.)
Replace: $1~$2
Something like this:
"()AAA-_BBB~_CCC=~DDD[]FFF{}GGG HHH~ III[].txt"
Renames to:
" AAA BBB~_CCC=~DDD FFF GGG HHH~ III .txt"
There is a Space a the beginning which I don't want
Chances are that there is a much better ways to do it... My 101 can't reach them yet
May be handy (and easier to update) to somehow declare a variable:
DirtyCharsList := -_()[]{},
To call later
Any help appreciated.
Cheers, Yogui.
]]>