简体   繁体   English

如何使用 powershell 导出 csv 中的文件属性

[英]How to export file properties in csv using powershell

I wanted to export a csv-File with File-Properties from some tif-Files.我想从一些 tif 文件中导出带有文件属性的 csv 文件。

With this command用这个命令

Get-ChildItem -Recurse C:\tifs\ | 
ForEach-Object {$_ | add-member -name "Owner" -membertype noteproperty `
-value (get-acl $_.fullname).owner -passthru} | Sort-Object fullname | 
Select FullName,CreationTime,LastWriteTime,Length,Dimensions | 
Export-Csv -Force -NoTypeInformation C:\Test\export.csv

I can export a csv just fine.我可以导出 csv 就好了。 But as soon as I want to add properties like vertical resolution it fails.但是,只要我想添加垂直分辨率等属性,它就会失败。 I don't quite understand why.我不太明白为什么。

In order to get to the "extended" file properties (like Dimension and Resolution metadata) you have to resort to using the Windows Visual Basic shell options from inside PowerShell as Steven helpfully pointed out.为了获得“扩展”文件属性(如尺寸和分辨率元数据),您必须求助于使用 Windows Visual Basic shell选项从Steven内部。 Here is a code sample that should give you the result:这是一个应该给你结果的代码示例:

$files = @()
$folder = (New-Object -ComObject Shell.Application).namespace("C:\tifs") 

# Loop through each file in folder
foreach ($f in $folder.Items()) {
  $a = 0

  # Print all the available properties (for debugging purposes)
  for ($a ; $a  -le 266; $a++) {  
    if($folder.GetDetailsOf($f, $a)) { 
      Write-Host "Property: $($folder.GetDetailsOf($folder.items, $a))"
      Write-Host "Value: $($folder.GetDetailsOf($f, $a))"
      Write-Host "Index: $($a)"
    }
  }

  # Store data in custom PowerShell object
  $obj = New-Object -TypeName PSOBJECT
  # Fill each property with the file metadata (by index number)
  $obj | Add-Member -MemberType NoteProperty -Name FullName -Value $folder.GetDetailsOf($f, 194)
  $obj | Add-Member -MemberType NoteProperty -Name CreationTime -Value $folder.GetDetailsOf($f, 4)
  $obj | Add-Member -MemberType NoteProperty -Name LastWriteTime -Value $folder.GetDetailsOf($f, 5)
  $obj | Add-Member -MemberType NoteProperty -Name Length -Value $folder.GetDetailsOf($f, 1)
  $obj | Add-Member -MemberType NoteProperty -Name Dimensions -Value $folder.GetDetailsOf($f, 31)
  # Add custom object to a collection
  $files += $obj
}

# Export collection to CSV
$files | Export-Csv -Force C:\Test\export.csv -NoTypeInformation -Encoding UTF8

From what I can tell there's no obvious PowerShell/.Net approach to getting additional file meta data.据我所知,没有明显的 PowerShell/.Net 方法来获取额外的文件元数据。 However.然而。 there are some COM based approaches.有一些基于 COM 的方法。

  1. Check Scripting Guys检查脚本专家
  2. And they reference this他们引用了这个

You will still have to correlate the data.您仍然需要关联数据。 I usually do that by building hash table keyed of same values, in you can index the metadata using the path property, then use the FullName property of the file info objects to reference it, so you can the properties.我通常通过构建 hash 表以相同的值键控来做到这一点,您可以使用 path 属性索引元数据,然后使用文件信息对象的 FullName 属性来引用它,这样您就可以使用这些属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM