如何使用perl检索文本文件的自定义属性

How to retrieve custom attribute of text file using perl

我有一个文本文件,我正在通过 Java 文件 API.
添加自定义属性 "email" 现在,我想从 perl 脚本中提取该属性。

在互联网上搜索后,我遇到了以下检索文件所有者的代码

use Win32::OLE;
   my $objShell = Win32::OLE->CreateObject("Shell.Application");
   my $objFolder=$objShell->Namespace("c:\temp") or die "$!" ;
   my $a = $objFolder->ParseName("Input.txt")  or die "$!" ;
   my $owninguser= $objFolder->GetDetailsOf($a, 10)."\n" or die "$!" ;

10 表示拥有 perl 5 及更高版本的用户。

我不能使用 Win32::File 因为它只是以 OR-edformat

检索常量

有人可以就如何检索文本文件的自定义属性提出一些建议。

顺便说一句,我正在使用 Windows 7 64 位和 Active Perl 5.18.4.1803

这很有趣。我的代码在最后。

我不太熟悉自定义文件属性,但下面将遍历所有属性并在遇到连续 2 个空格或所需键时停止。当 运行 时,您会发现它没有列出自定义 属性,因为它无法通过此界面使用。

这基于 How to get the details of a file on Windows

处的代码
use strict;
use warnings;

use Win32::OLE;

my $fileName = 'test.txt';
my $myDir = 'c:\testing\e';

my $objShell  = Win32::OLE->new('Shell.Application') or die;
my $objFolder = $objShell->NameSpace($myDir) or die;
my $objFile   = $objFolder->ParseName($fileName) or die;

my $blank = 0;
# Apparently the numbers vary between OS versions, 300 is a guess
for ( my $i = 0; $i < 300; $i++ ) {
  my $propertyName = $objFolder->GetDetailsOf($fileName, $i);
  # skip when finding 2 blanks in a row, there may be a few more later.
  if ($propertyName eq '') {
        if ($blank) {
            last;
        }
        $blank = 1;
        next;
  }
  my $propertyValue = $objFolder->GetDetailsOf($objFile, $i);
    print "$i -- $propertyName -- $propertyValue\n";
} 

所以,在浏览了 Internet 之后,我认为这对于普通的 Windows API 调用是不可能的(我对此可能是非常错误的)。

此 link 表明您可以从 Microsoft 下载 DSOFile.dll 以访问自定义属性:http://blog.rodhowarth.com/2008/06/how-to-set-custom-attributes-file.html

我试过下面的 perl 代码,它似乎可以工作。我没有深入研究 Win32::OLE 文档 - 可能有更好的方法来访问信息而不是直接哈希查找。

use strict;
use warnings;

use Win32::OLE;
use Data::Dumper;

my $obj = Win32::OLE->new('DSOFile.OleDocumentProperties') or die Win32::OLE->LastError;
$obj->Invoke('Open', 'C:\testing\e\test.txt', 0, 0); # constants found in headers
my $custom_property = $obj->{CustomProperties}{'myprop'}; # you would use 'email' here

my $custom_value = $custom_property->{'Value'};
print "Value is: $custom_value\n";

print "\nFile Object:\n", Dumper $obj;
print "\nCustom Property:\n", Dumper $custom_property;

您也可以直接使用 DLL,但这往往非常痛苦,因此最好避免。

DSOFile.dll 无法读取使用 Java 设置的自定义属性。
我花了两天时间测试了大约 15 个不同的文件后得出了这个结论。
所以请不要尝试在 Java 中设置用户定义的属性并通过 perl 读取它们。