views:

342

answers:

3

How can you list all files (recursively) within a directory where the file (audio) bit rate is greater than 32kbps using powershell?

+2  A: 

Well, the first part would definitely be a Get-ChildItem -Recurse. For the bit rate, you would need some more scripting, however. The Microsoft Scripting Guys answered a question to that a while ago: How Can I find Files' Metadata. You can probably use that to get to the audio bit rate and filter for that.

Joey
+1  A: 

In general, if you want to do something you know is natively supported by a built-in Windows component, the fastest route is likely to be COM. James Brundage has a great post on discovering these capabilities on the fly & quickly putting them to use.

Richard Berg
+1  A: 

Starting with the link from Johannes' post, here is a simple function that uses GetDetailsOf to find all mp3 files in a directory with a minimum bitrate:

function Get-Mp3Files( [string]$directory = "$pwd", [int]$minimumBitrate = 32 ) {
  $shellObject = New-Object -ComObject Shell.Application
  $bitrateAttribute = 0

  # Find all mp3 files under the given directory
  $mp3Files = Get-ChildItem $directory -recurse -filter '*.mp3'
  foreach( $file in $mp3Files ) {
    # Get a shell object to retrieve file metadata.
    $directoryObject = $shellObject.NameSpace( $file.Directory.FullName )
    $fileObject = $directoryObject.ParseName( $file.Name )

    # Find the index of the bit rate attribute, if necessary.
    for( $index = 5; -not $bitrateAttribute; ++$index ) {
      $name = $directoryObject.GetDetailsOf( $directoryObject.Items, $index )
      if( $name -eq 'Bit rate' ) { $bitrateAttribute = $index }
    }

    # Get the bit rate of the file.
    $bitrateString = $directoryObject.GetDetailsOf( $fileObject, $bitrateAttribute )
    if( $bitrateString -match '\d+' ) { [int]$bitrate = $matches[0] }
    else { $bitrate = -1 }

    # If the file has the desired bit rate, include it in the results.
    if( $bitrate -ge $minimumBitrate ) { $file }
  }
}
Emperor XLII