在 PowerShell 中枚举文件属性 [英] Enumerate file properties in PowerShell

查看:39
本文介绍了在 PowerShell 中枚举文件属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在其他问题中看到了一些这样的东西,但我正在寻找一种通用的方法来编写一个函数,该函数将接受一个文件,并以可以使用的方式列出其属性.我知道名为 Get-ItemProperty 的函数,但它没有列出我要查找的属性(例如,给定一个 .avi 文件,它不会告诉我长度、框架宽度、等).

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).

我是否使用了错误的函数(我所做的只是:Get-ItemProperty file)还是我必须以不同的方式执行此操作?

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

我希望能够对任意属性说诸如 $a += $file.Length 之类的内容.

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

推荐答案

听起来您正在寻找扩展文件属性.这些不存储在 System.IO.FileInfo 中.

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

一种方法是使用 Shell.Application COM 对象.下面是一些示例代码:

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-和-music.aspx

假设您有一个视频文件:C:\video.wmv

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)

您需要知道扩展属性的 ID 是什么.这将向您显示所有 ID:

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)

这篇关于在 PowerShell 中枚举文件属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆