Function Enumerate-Properties($fileName)
{
$path = (Get-Item $fileName).FullName
$shell = New-Object -COMObject Shell.Application
$folder = Split-Path $path
$file = Split-Path $path -Leaf
$shellfolder = $shell.Namespace($folder)
0..287 | Foreach-Object {'{0} = {1}' -f $_, $shellfolder.GetDetailsOf($null, $_)}
}
Function Find-Property($fileName, $PropertyName)
{
$shell = New-Object -COMObject Shell.Application
$path = (Get-Item $fileName).FullName
$folder = Split-Path $path
$file = Split-Path $path -Leaf
$shellfolder = $shell.Namespace($folder)
$shellfile = $shellfolder.ParseName($file)
$found = $false
0..287 | Foreach-Object {
$test1 = $shellfolder.GetDetailsOf($null, $_)
if($PropertyName -eq $shellfolder.GetDetailsOf($null, $_))
{
$found = $true
return $_
break
}
}
if(!$found)
{
Write-Host "Property "$PropertyName " was not found"
}
}
Function Get-PropertyValue($fileName, $property)
{
$shell = New-Object -COMObject Shell.Application
$path = (Get-Item $fileName).FullName
$folder = Split-Path $path
$file = Split-Path $path -Leaf
$shellfolder = $shell.Namespace($folder)
$shellfile = $shellfolder.ParseName($file)
return $shellfolder.GetDetailsOf($shellfile,$property)
}
Function Create-List3($files, $property)
{
$array = @()
$propertyNum = Find-Property $files[0] $property
foreach($file in $files)
{
$file | Add-Member -MemberType noteproperty -Name $property -Value (Get-PropertyValue $file $propertyNum).toString() -force
$array = $array + $file
}
return $array
}
This code is designed to be used in something like the following fashion:
$files = dir *.mp4
$propery = "Bit rate"
$array = Create-List3 $files $property
$array | Format-Table Name, Length, $property -auto
As far as I can tell, this code works when you give it a nice amount of .mp4 files. It fares less well when handed other things (folders and the like), the break statement in the Find-Property does nothing (and there has to be a better way to do that anyway).
I am mostly concerned that while this is valid powershell code, it is very poor powershell code, in that it is written in a psudo-C# style (that is where I am coming from) and not a powershell style.
Additionally, I am positive that there easier ways to set up the COM Objects that I am using, and parsing filenames (and reusing them, but that is more complex probably).
How can I make this code better?
Edit: Final code:
Function Create-ShellFolder($file)
{
$shell = New-Object -COMObject Shell.Application
$folder = Split-Path $file.FullName
return $shell.Namespace($folder)
}
Function Enumerate-Properties($fileName)
{
$shellfolder = Create-ShellFolder $fileName
0..287 | Foreach-Object {'{0} = {1}' -f $_, $shellfolder.GetDetailsOf($null, $_)}
}
Function Get-PropertyValue($fileName, $property)
{
$shellfolder = Create-ShellFolder $fileName
$shellfile = $shellfolder.ParseName($fileName)
return $shellfolder.GetDetailsOf($shellfile,$property)
}
Function Find-Property($fileName, $PropertyName)
{
$shellfolder = Create-ShellFolder $fileName
0..287 | Foreach-Object {
if($PropertyName -eq $shellfolder.GetDetailsOf($null, $_)){ return $_ }
}
}
Function Create-List($files, $property)
{
$array = @()
$propertyNum = Find-Property $files[0] $property
foreach($file in $files) {
$file | Add-Member -MemberType noteproperty -Name $property -Value (Get-PropertyValue $file $propertyNum).toString() -force
$array = $array + $file
}
return $array
}