文件日期元数据未正确显示 [英] File date metadata not displaying properly

查看:18
本文介绍了文件日期元数据未正确显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试基于一些 在线代码,它将从图片、视频和其他文件中读取元数据信息,然后根据日期之一对它们进行排序(目前采取的日期似乎是最好的打赌它是否可用,但修改日期适用于尚未更改的文件).

I have been trying to write a Powershell script based on some code online that will read the metadata info from picture, video and other files and then sort them based on one of the dates (date taken currently seems to be the best bet if it's available, but date modified works on files that have not yet been altered).

但是,当我运行脚本并提取信息时,我无法将字符串转换为日期.以下是我通过 COM 对象获取信息的大致方法:

However, when I run the script and pull the info, I can't convert the string to a date. Here's roughly how I get the info through a COM object:

PS C:/> $objShell = New-Object -ComObject Shell.Application
PS C:/> $objFolder = $objShell.namespace("C:MyFolder")
PS C:/> $date = $objFolder.GetDetailsOf($objFolder.Items().Item(0), 12)
PS C:/> $date

7/‎10/‎2014 ‏‎7:09 PM

问题是我应该能够将其转换为日期时间对象.例如,如果我手动将其写入其中:

The problem is I should be able to convert this to a datetime object. For instance, if I manually write it in it works:

PS C:/> [datetime]::ParseExact("7/10/2014 7:09 PM","g",$null)

Thursday, July 10, 2014 7:09:00 PM

但如果我替换变量它不起作用:

But if I substitute the variable it doesn't work:

PS C:/> [datetime]::ParseExact($date,"g",$null)
Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."
At line:1 char:1
+ [datetime]::ParseExact($date,"g",$null)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FormatException

这很可能是因为该变量实际上并不是我所看到的.实际上更长.更不用说如果你遍历所有字符,你可以看到额外的长度是从哪里来的:

This is most likely due to the fact that the variable isn't actually what I'm seeing. It's in fact longer. Not to mention the fact that if you iterate through all the characters, you can see where the extra length is coming from:

PS C:> $date.Length #should be about 16, you'd think

22

PS C:> $datearray = @()
PS C:> for ($i = 0; $i -lt $date.Length; $i++) {$datearray += $date[$i]}
PS C:> $datearray #i'm printing on one line and in quotes for ease of viewing

" 7/ 10/ 2014   7:09 PM"

如果您尝试使用连接或类似方法打印数组,结果(对我来说,不知道发生了什么)是不可预测的.它将它视为有 22 个字符,但打印时会忽略空格.

If you try printing the array with a join or something similar, the results are (to me, without knowing what's going on) unpredictable. It treats it like it has 22 characters, but prints ignoring the spaces.

我确信我可以花点时间做一些字符串格式化,但我宁愿只能够解析给定的日期.怎么回事?

I'm sure I could spend a bit of time and do some string formatting, but I'd rather just be able to parse the given date. What's going on?

我可以轻松访问文件信息,但我不喜欢这样做.我主要关注为什么我看到的结果是不稳定的,并且显示的长度与它的打印方式不匹配,以及我如何处理它们.如果不出意外,我很好奇发生了什么.

I'm able to access the file info easily, though I prefer not to. I'm mainly focusing on why the results I'm seeing are inconstant and showing a length that doesn't match how it prints out, and how I can deal with them. If nothing else, I'm curious as to what is going on.

推荐答案

我想知道多余的字符是什么(你没有提到已经在看这个.).我将您的数组代码 $datearray += $date[$i] 更新为 $datearray += [int][char]$date[$i].truncated 输出显示了两个奇怪的 82078206 转换为从左到右的标记和从右到左的标记.它们通常与 html 相关联.不幸的是,我无法洞察他们的存在.好消息是它们很容易移除.

I wanted to know what the extra characters were ( you dont mention already looking at this. ). I Updated your array code $datearray += $date[$i] to $datearray += [int][char]$date[$i]. The truncated output showed two oddities 8207 and 8206 which translate to left-to-right mark and right-to-left mark. They are normally associated with html. Unfortunately i cannot provide insight to their presense. Good news is that they are easy to remove.

$date = ($date -replace [char]8206) -replace [char]8207
[datetime]::ParseExact($date,"g",$null)

净输出

Thursday, March 26, 2009 1:43:00 PM

希望这更接近你想要的.我尝试寻找这些 ascii 代码存在的原因,但我没有发现任何有用的东西.

Hopefully this is a little bit closer of what you wanted. I tried searching for reasons for the presence of those ascii codes but i didn't find anything useful.

其他读者的额外信息

我这样做是因为我不知道索引 12 是什么.所以我创建了一个数组,其中包含所有可能的文件元数据的友好名称(288 个条目!).为简洁起见,我在 here 有此处的字符串.

I did this since i didnt know what the index 12 was. So i made an array that contains the friendly names of all the file meta data possible (288 entries!). I have the here-string located here for brevity.

有了这个,我正在根据我的图片测试以下代码.

With this i was testing the following code against a picture of mine.

$objShell = New-Object -ComObject Shell.Application
$objFolder = $objShell.namespace("C:Temp")
0..$Meta.GetUpperBound(0)| %{
    $metaValue = $objFolder.GetDetailsOf($objFolder.Items().Item(2), $_)
    If ($metaValue) {Write-Host "$_ - $($meta[$_]) - $metaValue"}
    } 

$Meta 是我之前提到的数组.代码将循环浏览我的文件的所有详细信息,由 Item(2) 指示,将包含值的所有文件详细信息写入屏幕.最后有一行将字符串转换为日期值.下面的脚本输出

$Meta is the array i spoke of earlier. The code will cycle though all the details of my file, indicated by Item(2), writing to screen all file details that contain values. In the end there is a line converting the string to a date value. Script output below

0 - Name - IMG_0571.JPG
1 - Size - 3.12 MB
2 - Item type - JPEG image
3 - Date modified - 3/26/2009 3:34 PM
4 - Date created - 8/24/2014 5:19 PM
5 - Date accessed - 8/24/2014 5:19 PM
6 - Attributes - A
9 - Perceived type - Image
10 - Owner - TE_STCameron
11 - Kind - Picture
12 - Date taken - ‎3/‎26/‎2009 ‏‎1:43 PM
19 - Rating - Unrated
30 - Camera model - Canon EOS DIGITAL REBEL XS
31 - Dimensions - ‪3888 x 2592‬
32 - Camera maker - Canon
53 - Computer - TE_ST (this computer)
155 - Filename - IMG_0571.JPG
160 - Bit depth - 24
161 - Horizontal resolution - ‎72 dpi
162 - Width - ‎3888 pixels
....output truncated....
247 - Program mode - Normal program
250 - White balance - Auto
269 - Sharing status - Not shared

这篇关于文件日期元数据未正确显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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