Skip to main content

Notice

Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: Extracting a particular directory path (Read 3754 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

Extracting a particular directory path

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: [Select]
$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.

Extracting a particular directory path

Reply #1
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.

Extracting a particular directory path

Reply #2
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: [Select]
$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)))

Extracting a particular directory path

Reply #3
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.

Extracting a particular directory path

Reply #4
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: [Select]
$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: [Select]
$if($and(%artist%,$greater(36,$len(%artist%))),%artist%,
$directory(%path%,$sub($sub($len(%path%),$len($replace(%path%,\,))),2)))

Hope this helps.

Extracting a particular directory path

Reply #5
-> $len($replace(%path%,\,))
Brilliant idea.

Extracting a particular directory path

Reply #6
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.

Extracting a particular directory path

Reply #7
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)

Extracting a particular directory path

Reply #8
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.   

Extracting a particular directory path

Reply #9
Yes, you've got it right.