Counting subdirectories with re_getCount

Use this forum for everything concerning script usage
Post Reply
MarcusE
Newbie
Newbie
Posts: 2
Joined: Thu Mar 07, 2019 8:09 pm

Counting subdirectories with re_getCount

Post by MarcusE »

Hello,

let me first say thanks for this great tool to the developper!!!
I really must say, it was a great help - a highly sopisticated one!!

I am stumbling on a particular 'problem', maybe someone has an idea or maybe this could be an additional function.
I'm tring to restructure a large library with a script, I built/adopted from #advanced.scu, including the basic structure
{!%*?A;AA;SR,1,1%+%?A;AA;SR%+(%Y%) %L%}.

I found out, that the use of re_getcount in a restructure script does count file fine, but ingore existing (sub-)directories.
In the #advanced.scu, albums with only one song/file are ignored in the structure, file are shifted to the next higher directory.
Same, if the artist has only one song in his directory.

But exactly here, (sub-)directories for albums are ignored.

Result:
/A/ABBA/(1985) the best of/name of the game.mp3
/A/ABBA/(1985) the best of/mama mia.mp3
/A/Abba - super trooper.mp3 (file has no ablum tag or it is the single file with particular album tag)
Here, when counting files in ABBA directory, the subdirectories are ignored.

Do you catch my point?
I can certainly submit my script for review.

May thanks for any comment.

Best regards
Marcus

User avatar
jtclipper
Administrator
Administrator
Posts: 768
Joined: Tue Aug 10, 2010 12:04 pm

Re: Counting subdirectories with re_getCount

Post by jtclipper »

The particular script will remove structural levels for just one song.
It uses the re_removeLevel to do that and defaults to the previous available structure level.
This can be easily amended by removing those lines of code and all songs will end up in their specific sub directory even if it only contains one file.

If this is the desired result you can post the script here and I can guide you through the solution

MarcusE
Newbie
Newbie
Posts: 2
Joined: Thu Mar 07, 2019 8:09 pm

Re: Counting subdirectories with re_getCount

Post by MarcusE »

Hi,

thanks for your offer of support and understand your approach.
My point is:
Some file are found in the initials folder (under A for my example with ABBA), even if ABBA have it own folder with albums.
This happens because file are understood as 'alone' even if there are albums and the artist folder will persist.

If I remove those lines according to your proposal, every single song gets an artist folder - not wanted.

My main target is to keep a single file in the artists folder, if there are albums.


I found myself a workaround in the meantime...

I changed the basic structure template to {!%*?A;AA;SR,1,1%+%?A;AA;SR%}.
Like this, no album folders are built in the structure.

In phase 1, artist name are corrected.
Here I remove THE, DER, DIE and DAS at the beginning, and I make profit of a replacement matrix:
- ä --> ae, ö --> oe, ü --> ue, some french letters, etc
- unwanted chars like ',.;:-_|!"°^<>/=`´ --> blank
- three blanks --> one blank, two blanks --> one blank
- none of the regex, none case sensitive is marked

In phase 2, I can now count the files in the artist folder correctly, including files which will be moved to album folders later in phase 3.
In case there is only one song/file for particular artist, the artist level (2) is removed. No subdir for this artist.

In phase 3, I check through all remaining artist folders.
If there is year or album for a particular song/file, then album level (3) is added via gTag.Year and gTag.Album.

In phase 4, I can now count the files in each album folder.
In case there is only one song/file for that particular year or album, album level (3) is removed again.

This is it. I checked the function and performance.
Function is great!! Performance is really acceptable.

I Even Added A 'Capitalize' Function At The Top.

Enjoy!!

Code: Select all

{!%*?A;AA;SR,1,1%+%?A;AA;SR%}
// the first line contains the folder structure and is dynamically used upon script execution, it is optional

{
  sample script to force special folder rules
  the folder structure defined is the following :
  1 - %*AR,1,1% - artist first letter
  2 - %AR%      - artist
  3 - %Y% %L% - (year) album - built in phase 3
}

program MyPersonal_folder_rules;

function Capitalize (const s: string): string;

var flag: boolean;
   i : Byte;
   t : string;
begin
 if s<>'' then
    begin
      flag := true;
      t := '';
      for i := 1 to Length(s) do
      begin
        if flag then
           t := t+ UpperCase(s[i])
        else
           t := t+ s[i];
        flag := (s[i] = ' ')
      end;
    end
 else t:='';
 result := t;
end;


const
  _MiscFolder = '_Diverse'; // my misc folder
  matrix = 0;
var
   sTmp2, sTmp3, album, year: string;
begin
// phase 1: Loop through artist names and adjust values as needed
  re_Init( false );
  repeat
     sTmp2 := trim(sys_ApplyMatrix( matrix,re_getLevel( 2 )));
     if UpperCase( Copy( sTmp2, 1, 4 ) ) = 'THE ' then begin // remove 'the'
        sTmp2 := Copy( sTmp2, 5, 9999 );
     end;
     if UpperCase( Copy( sTmp2, 1, 4 ) ) = 'DER ' then begin // remove 'der'
        sTmp2 := Copy( sTmp2, 5, 9999 );
     end;
     if UpperCase( Copy( sTmp2, 1, 4 ) ) = 'DIE ' then begin // remove 'die'
        sTmp2 := Copy( sTmp2, 5, 9999 );
     end;
     if UpperCase( Copy( sTmp2, 1, 4 ) ) = 'DAS ' then begin // remove 'das'
        sTmp2 := Copy( sTmp2, 5, 9999 );
     end;

     if Copy( sTmp2, 1, 1 ) = ' ' then
        sTmp2 := Copy( sTmp2, 2, 9999 );

     re_setLevel( 2, Capitalize( sTmp2 ));
     re_setLevel( 1, Uppercase(Copy( sTmp2, 1, 1 )));

     if Pos( Copy( sTmp2, 1, 1 ), '1234567890.''' ) > 0 then begin // move all those artists to custom folder
        re_setLevel( 1, _MiscFolder );
     end;

  until not re_Skip;


// phase 2: Get count and loop trough artists and REMOVE LEVEL IF ONLY ONE FILE PER ARTIST
// we have to do the re-count of files per folder here because the structure may have been altered above

  re_Init( true );
  repeat
     if re_getCount = 1 then // no Artist folder for just one song put it in the root letter folder ( A B C ... )
        re_removeLevel( 2 );
  until not re_Skip;


// phase 3: CREATE ALBUMS in artist folders for all files with year or album info

   re_Init( false );
   repeat
     if length(re_getlevel(2))>0 then begin
        re_LoadFile;
        sTmp3 := '';
        Album := trim(sys_ApplyMatrix( matrix,gTag.Album));
        Year  := gTag.Year;
        if length(Year) > 0 then begin
           sTmp3 := '(' + Year + ')';
           if length(Album) > 0 then
              sTmp3 := sTmp3 + ' ' + Album;
           end
        else if length(Album) > 0 then
           sTmp3 := Album;
        if length(sTmp3) > 0 then
           re_addLevel( sTmp3 );
     end;
  until not re_Skip;

// phase 4: Get count and loop through album folders REMOVE LEVEL IF ONLY ONE FILE PER ALBUM
// we have to do the re-count of files per folder here because the structure may have been altered above

  re_Init( true );
  repeat
     if length(trim(re_getLevel( 3 ))) > 0 then // check only for album folders
        if re_getCount = 1 then // no Artist folder for just one song put it in the artist folder ( ABBA... )
           re_removeLevel( 3 );
  until not re_Skip;

end.

User avatar
jtclipper
Administrator
Administrator
Posts: 768
Joined: Tue Aug 10, 2010 12:04 pm

Re: Counting subdirectories with re_getCount

Post by jtclipper »

Glad to see you got it working, I had a quick look and it should behave as expected.
When I created the advanced.scu I had all my files tagged more or less correctly so I did not notice this issue, an extra check at the end might make it a bit more robust but your approach utilizing re_addlevel also works very well.

Post Reply