问题
How can you list all files (recursively) within a directory where the file (audio) bit rate is greater than 32kbps using powershell?
回答1:
Starting with the link (new link location) 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 }
}
}
回答2:
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.
回答3:
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.
回答4:
Stupendous Emperor! I realized that I had some 56K MP3s that I should replace with better quality (heh, they were ripped when drives were measured by hundreds of megs, not gigs, saving space was what mattered!) And I thought this morning that I should probably see if there's a way to do that under Powershell (I used to have a program called EDIR that would get that info from files, but that was back in the days of Win 95...), and this page was the first hit under powershell OR monad mp3 bitrate extraction. Talk about getting lucky!
I could also figure out to change this so I can go through and pick all the wallpapers that are 16:9, 16:10 or thereabouts, if nothing else just to categorize them. 16:9-10, 4:3 (portrait), 9-10:16, 3:4 (landscape), square, and other. Sounds like a plan to me in my (ha!) spare time.
来源:https://stackoverflow.com/questions/1153819/get-list-of-files-recursively-by-bit-rate-in-powershell