使用 Powershell 获取“详细信息”选项卡中列出的属性

问题描述 投票:0回答:4

我正在尝试使用 PowerShell 检索“详细信息”选项卡“媒体”部分中列出的一些扩展文件属性(准确地说是 mp3 文件)。

还有其他属性,如

bitrate
genre
album

可悲的是

Get-ItemProperty -Path $pathtofile | Format-List
不归还那些。

Get-ItemProperty -Path $pathtofile | Get-Member
也没有列出它们。

很奇怪,

Get-ItemProperty
返回与
Get-ChildItem
相同的输出。
我尝试过不同的文件(甚至非 mp3),并且 PowerShell 没有在任何地方列出“详细”属性。

Windows 将这些存储在哪里?还有如何列出它们?

file powershell attributes id3v2
4个回答
3
投票

我看了埃德·威尔逊的帖子。我可以看到他当天在 Windows Vista 上写的。从那时起,Powershell 发生了一些变化。我已经调整了他编写的代码以在 Win 10 上运行。希望这会有所帮助!

 # Converted from Ed Wilson: https://devblogs.microsoft.com/scripting/hey-scripting-guy-how-can-i-find-files-metadata/

param($folder = "C:\Test") #end param

function funLinestrIN($strIN)
{
     $strLine = "=" * $strIn.length
     Write-Host "`n$strIN" -ForegroundColor Yellow 
     Write-Host $strLine -ForegroundColor Cyan
} #end funline

function funMetaData
{
    foreach($sFolder in $folder)
         {
              $a = 0
              $objShell = New-Object -ComObject Shell.Application
              $objFolder = $objShell.namespace($sFolder)

              foreach ($strFileName in $objFolder.items())
                   { 
                       funLinestrIN( "$($strFileName.name)")
                       for ($a ; $a  -le 266; $a++)
                          { 
                                if($objFolder.getDetailsOf($strFileName, $a))
                                  {
                                        $hash += @{ `
                                              $($objFolder.getDetailsOf($objFolder.items, $a)) = $($objFolder.getDetailsOf($strFileName, $a)) 
                                              } #end hash
                                       $hash
                                       $hash.clear()
                                  } #end if
                            } #end for 
                        $a=0
                    } #end foreach
        } #end foreach
} #end funMetadata
funMetaData # run function

2
投票

更新3; 我发现了一个更好的脚本,它应该完全按照令人难以置信的“Hey!Scripting Guy”博客提供的方式执行您想要的操作。他们已经构建了一个功能,可以让您查看音乐/mp3 文件的所有详细信息。

博客文章
https://blogs.technet.microsoft.com/heyscriptingguy/2014/02/05/list-music-file-metadata-in-a-csv-and-open-in-excel-with-powershell/

功能
https://gallery.technet.microsoft.com/scriptcenter/get-file-meta-data-function-f9e8d804


1
投票

这个答案只是为了修复 Nick W 答案中的链接。微软杀死了 TechNet,但脚本位于 PowerShell Gallery 上。

我用谷歌搜索了以下内容来找到它: PowerShell“获取文件元数据”

地点是: https://www.powershellgallery.com/packages/FC_SysAdmin/5.0.0/Content/public%5CGet-FileMetaData.ps1

不幸的是,我无法让它工作,哈哈。


0
投票

基于 JefNet 答案。

如果您只想要一个特定文件的信息,则迭代目录树中的每个文件就太多了(对于大型目录需要很长时间),所以这里是这个。

获取所有信息(包括空字段):

$objShell = New-Object -ComObject Shell.Application
$objFolder = $objShell.Namespace((Get-Item .).FullName)

$filenameWithExtension = "example.mp3"

$objFile = $objFolder.ParseName($filenameWithExtension)
$fileMeta = [ordered]@{}
for($id = 0; $id -le 266; $id++){
    $fileMeta[ $($objFolder.GetDetailsOf($objFolder, $id)) ] = $(
        if($objFolder.GetDetailsOf($objFile, $id)){
            $($objFolder.GetDetailsOf($objFile, $id))
        }else{ "" }
    )
}

# print ordered hashtable
$fileMeta

仅获取非空字段:

$objShell = New-Object -ComObject Shell.Application
$objFolder = $objShell.Namespace((Get-Item .).FullName)

$filenameWithExtension = "example.mp3"

$objFile = $objFolder.ParseName($filenameWithExtension)
$fileMeta = [ordered]@{}
for($id = 0; $id -le 266; $id++){
    if($objFolder.GetDetailsOf($objFile, $id)){
        $fileMeta[
            $($objFolder.GetDetailsOf($objFolder, $id))
        ] = $($objFolder.GetDetailsOf($objFile, $id))
    }
}

# print ordered hashtable
$fileMeta

PS.:

$($objFolder.GetDetailsOf($objFolder, $id))
以系统语言打印字段名称,并不总是英文,所以如果需要与不同语言保持一致,请使用
$id
作为键。

PPS.:如果你只想要一个字段值,用这个替换哈希表和for循环

$fileMetaAuthors = $(
    # 20 is the "Authors" field
    if($objFolder.GetDetailsOf($objFile, 20)){
        $($objFolder.GetDetailsOf($objFile, 20))
    }else{ "" }
)

# print "Authors" of the file
$fileMetaAuthors

PPPS.:所有这些都期望文件位于当前文件夹中。如果不是这种情况,请修改

Get-Item .
以指向正确的文件夹。

© www.soinside.com 2019 - 2024. All rights reserved.