Help - Search - Members - Calendar
Full Version: Extracting a particular directory path
Hydrogenaudio Forums > Hosted Forums > foobar2000 > Support - (fb2k)
Airborne
I'm trying to do the following:

If the artist tag is present, display the artist tag.
If the artist tag is missing, or is longer than 35 characters, use the name of the artist-level folder on my hard drive.

For example, consider the following file structure:

mp3s
|
|__OldiesFolder
.....|
.....|___SongOne.mp3
.....|
.....|___Subfolder
............|
............|__SongTwo.mp3

Assuming both SongOne and SongTwo have no artist tags, I'd like them both to display "OldiesFolder"

I have a working solution, below, but it's horribly inefficient, and just goes up one step at a time until it hits the "mp3s" folder, then returns the folder one level down from that. It works, but it increases load time on longer playlists. Is there a better way to do this?

CODE
$puts(artistDIR,
$if($strcmp($directory(%path%),'mp3s'),,
$if($strcmp($directory(%path%,1),'mp3s'),$directory(%path%),
$if($strcmp($directory(%path%,2),'mp3s'),$directory(%path%,1),
$if($strcmp($directory(%path%,3),'mp3s'),$directory(%path%,2),
$if($strcmp($directory(%path%,4),'mp3s'),$directory(%path%,3),
$if($strcmp($directory(%path%,5),'mp3s'),$directory(%path%,4),
$if($strcmp($directory(%path%,6),'mp3s'),$directory(%path%,5),
)
)
)
)
)
)
)
)
$iflonger(%artist%,35,$get(artistDIR),$if2(%artist%,$get(artistDIR)))


I just realized perhaps this better belongs in a columns_ui thread rather than in here...

I thought about posting in both, but couldn't figure which would be better. Mods, please don't crucify me for this! I got warned back in the day when I was brand new to this, and I don't mean to misbehave. Please move this thread if it's improperly placed.
Squeller
You can use $strstr() to find the position of "\mp3s\" in your %path%. Add 6 and you have the start position of the folder you are searching for. In the string from there to the end, you then have to search for the first occurence ($strchr) of "\", which is the end of your folder then ... Some scissors, some glue, and you have your string... Just an idea.
Airborne
QUOTE(Squeller @ Aug 22 2006, 04:05) *

You can use $strstr() to find the position of "\mp3s\" in your %path%. Add 6 and you have the start position of the folder you are searching for. In the string from there to the end, you then have to search for the first occurence ($strchr) of "\", which is the end of your folder then ... Some scissors, some glue, and you have your string... Just an idea.


Thanks for the input! The following code isn't working for me ... any thoughts?

CODE
$puts(beg,$add($strstr(%path%,$strstr(%path%,\mp3s\)),6))
$puts(end,$strchr($substr(%path%,$get(beg),$len(%path%)),\))
$puts(artistDIR,$substr(%path%,$get(beg),$get(end)))
$iflonger(%artist%,35,$get(artistDIR),$if2(%artist%,$get(artistDIR)))
Squeller
My bad: To get the first occurence position you need $strchr().
BTW, instead of calculating the paths length in the $substr command you can simply enter a high value (9999) to save one operation.
Frank Bicking
I have posted a solution to reverse $directory some weeks ago.

Assuming you have a directory structure like this:

D:\MP3s\OldiesFolder\SongOne.mp3
D:\MP3s\OldiesFolder\Subfolder\SongTwo.mp3

The following code will always return the name of the blue folder:

CODE
$directory(%path%,$sub($sub($len(%path%),$len($replace(%path%,\,))),2))

And to solve your whole problem:
QUOTE
If the artist tag is present, display the artist tag.
If the artist tag is missing, or is longer than 35 characters, use the name of the artist-level folder on my hard drive.

CODE
$if($and(%artist%,$greater(36,$len(%artist%))),%artist%,
$directory(%path%,$sub($sub($len(%path%),$len($replace(%path%,\,))),2)))

Hope this helps.
Squeller
-> $len($replace(%path%,\,))
Brilliant idea.
Airborne
Frank: Superb! Thanks so much for that brilliantly elegant solution. Let me see if I get the logic right:

It essentially counts the number of \ characters in the path, and then goes up that many levels (getting to the root). The subtraction of 2 at the end is because I want to be two levels down from the root, right?

Just beautiful. One line ... I love it. You're very creative.
Squeller
Because $directory(%path%,n) is the <n>th directory from the right, he simply counts all levels (the inner substraction) and substracts the wanted left aligned level number from this value.

So in this example:

c:\blah\blubb\blubber\laber\raber\rabarber\oerks.mp3

you have 7 backslashes, if you want to access the third folder "blubber", you need to access $directory(%path%,allbackslashes-three) = $directory(%path%,4)
Airborne
QUOTE(Squeller @ Aug 23 2006, 03:06) *

Because $directory(%path%,n) is the <n>th directory from the right, he simply counts all levels (the inner substraction) and substracts the wanted left aligned level number from this value.

So in this example:

c:\blah\blubb\blubber\laber\raber\rabarber\oerks.mp3

you have 7 backslashes, if you want to access the third folder "blubber", you need to access $directory(%path%,allbackslashes-three) = $directory(%path%,4)


A simple "yes, you've got it right" would have done. rolleyes.gif tongue.gif
Squeller
Yes, you've got it right.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.