文件日期元数据无法正常显示 [英] File date metadata not displaying properly

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

问题描述

我一直试图写一个Powershell脚本基于一些代码在线,它将从图片,视频和其他文件中读取元数据信息,然后根据其中一个日期排序(目前采用的日期似乎是最好的如果它是可用的,但日期修改作品文件尚未更改)。

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

问题是我应该能够将其转换为datetime对象。例如,如果我手动写入它工作:

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 ] 输出显示两个异常 8207 8206 ,它们从左到右标记和从右到左标记。他们通常与html相关联。不幸的是,我不能提供洞察他们的presense。好消息是,他们很容易删除。

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)

>

Which nets the output

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.

其他读者的额外资料

Extra Information for other readers

索引 12 是。所以我做了一个数组,包含所有的文件元数据可能的友好名称(288项!)。我有一个位于此处的简明字符串。

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_ST\Cameron
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天全站免登陆