Can I get subproperties based on their value?

217 Views Asked by At

I am trying to get my music organized. So i want to remove unnecessary tags and only have "contributing artists" tags, instead of a mixture of "contributing artists" and "composers"

I am using Taglib-Sharp for this.

So after this:

Add-Type -Path "$PWD\taglib-sharp.dll"
cd "$Home\Music"
$songs = ((Get-ChildItem *.mp3 -Recurse).Fullname)
$taglib = ($songs | % {[TagLib.File]::Create($_)})

I have my music library loaded inside $taglib. Now I'd like to search for ALL sub property's, that include stuff like "link.com". For example: $taglib has the properties "length", "name", "tag" [...]. While "tag" itself has "Artist", "Album", "Name" [...] properties.

So I want to iterate through all all sub properties of every element/song in $taglib and find a property, based on it's value. I've tried to make a function that takes an object and recursively calls itself whenever the object has property's:

function AllProps($obj){ 
    foreach($member in (gm -inputobject $obj -membertype property).name){
        if($member -eq $null) {
            $obj            
        } else {
            "$member"
            AllProps -obj $obj.$member
        }
    }
}

But it get stuck in a loop ... Is there an easy way out? TIA!

1

There are 1 best solutions below

1
Wasif On

Try something like this:

filter ExpandProperty {
    $obj = $_
    $obj | get-member -MemberType *property | foreach {
        #Find objects with array value
        if( @($obj.($_.Name)).Count -gt 1 ) {
            $count = 1
            $prop = $_.Name
            $obj.($prop) | foreach {
                #Foreach value in the property
                $obj | Add-Member NoteProperty -Name "$prop (Property $($count))" -Value $_
                $count++
            }
        }
    }
    #Output object
    $obj
}

$taglib | ExpandProperty