#11 2009-06-07 17:12

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

Re: Rearrange and Rename Folder Name Artist Album Pascal Script

One appointment,Yogui , on the RegEx rules the symbol '|' when it's betwen [ ] doesn't work  as it does inside ( )

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

Last edited by SafetyCar (2009-06-07 17:14)


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

Offline

#12 2009-06-07 20:50

prologician
Member
Registered: 2009-01-30
Posts: 84

Re: Rearrange and Rename Folder Name Artist Album Pascal Script

The "Number to Roman Numeral" function I wrote (ie, the second one) hasn't been fitted to match the specific situation here, Yogui. Currently, the way it's written, it interprets the entire filename as a number and attempts to make it into a roman numeral. If the conversion fails for some reason (ie, the number is not in the range of 0-3999), the empty string is returned.... which is exactly what you're seeing. To make it apply for what you're doing, you'd need to replace the line "filename := NumberToRoman(strtoint(filename));" with something that picks out the numbers you want to change to roman numerals, pass then to NumberToRoman, and then take the output and somehow merge it back into the filename. (You can get an idea of how this is done by looking at the RomanToNumber script.) smile

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. smile

Last edited by prologician (2009-06-07 21:00)

Offline

#13 2009-06-08 03:58

Yogui
Member
Registered: 2007-09-27
Posts: 41

Re: Rearrange and Rename Folder Name Artist Album Pascal Script

Thanks SafetyCar & prologician,

@ SafetyCar

I didn't know about the \ inside the [] roll Thanks for fixing my RegEx smile

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? hmm

@ prologician

My RegEx Skills are not enough to even try, but I'll keep the function to later. tongue

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. big_smile

Offline

#14 2009-06-08 07:48

prologician
Member
Registered: 2009-01-30
Posts: 84

Re: Rearrange and Rename Folder Name Artist Album Pascal Script

Well, I included/mentioned the Number-->Roman Numeral script more for completeness rather than what you're trying to do. This way, if someone down the line needs to look up Roman numeral stuff, it's all in one thread rather than spread around. wink

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. smile Change the detecting regular expression and the argument of the WideReplaceText function. smile As such, I don't see any particular need to post these edits. smile

Offline

#15 2009-06-08 16:28

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

Re: Rearrange and Rename Folder Name Artist Album Pascal Script

Yogui wrote:

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? hmm

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


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

Offline

#16 2009-06-09 14:12

Yogui
Member
Registered: 2007-09-27
Posts: 41

Re: Rearrange and Rename Folder Name Artist Album Pascal Script

Hi prologician,

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. roll

Hope your holidays are not over yet smile

Cheers, Yogui.

Offline

#17 2009-06-10 10:39

prologician
Member
Registered: 2009-01-30
Posts: 84

Re: Rearrange and Rename Folder Name Artist Album Pascal Script

A fun five-minute tweak, but not too bad. I removed the subdirectory check, because it's not really applicable to files.

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." wink 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...). wink 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.

Last edited by prologician (2009-06-10 10:41)

Offline

#18 2009-06-10 12:00

Yogui
Member
Registered: 2007-09-27
Posts: 41

Re: Rearrange and Rename Folder Name Artist Album Pascal Script

thanks a lot prologician!

Yes I'll take note "...I'm also holding you to your statement..." big_smile

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

Offline

#19 2009-06-10 21:00

prologician
Member
Registered: 2009-01-30
Posts: 84

Re: Rearrange and Rename Folder Name Artist Album Pascal Script

Hehe. It shows that your code really is untested... it gives an error right when you press the "Try to Compile" button.

"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. wink

<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. smile

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. smile

Offline

#20 2009-06-11 08:58

Yogui
Member
Registered: 2007-09-27
Posts: 41

Re: Rearrange and Rename Folder Name Artist Album Pascal Script

I didn't even try... I wasn't too far after all wink

It's much easier with your comments.

NOW IS TESTED BUT DON'T TRUST IT... roll

{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.

Offline

Board footer

Powered by FluxBB