Enumerate file properties in PowerShell

北城以北 提交于 2019-11-28 04:13:40

问题


I have seen bits of this in other questions, but I am looking for a generic way to write a function that will take a file, and list its properties in such a way that they can be used. I am aware of the function called Get-ItemProperty but it does not list the properties that I am looking for (for example, given a .avi file, it will not tell me the length, frame width, etc).

Am I using the function wrong (all I am doing is: Get-ItemProperty file) or do I have to do this a different way?

I want to be able to say something like $a += $file.Length, or something like that for arbitrary properties.


回答1:


Sounds like you are looking for extended file attributes. These are not stored in System.IO.FileInfo.

One way is to use the Shell.Application COM object. Here is some example code:

http://web.archive.org/web/20160201231836/http://powershell.com/cs/blogs/tobias/archive/2011/01/07/organizing-videos-and-music.aspx

Say you had a video file: C:\video.wmv

$path = 'C:\video.wmv'
$shell = New-Object -COMObject Shell.Application
$folder = Split-Path $path
$file = Split-Path $path -Leaf
$shellfolder = $shell.Namespace($folder)
$shellfile = $shellfolder.ParseName($file)

You'll need to know what the ID of the extended attribute is. This will show you all of the ID's:

0..287 | Foreach-Object { '{0} = {1}' -f $_, $shellfolder.GetDetailsOf($null, $_) }

Once you find the one you want you can access it like this:

$shellfolder.GetDetailsOf($shellfile, 216)


来源:https://stackoverflow.com/questions/9420055/enumerate-file-properties-in-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!