【发布时间】:2018-10-15 16:15:58
【问题描述】:
我正在使用 powershell 从 Jpg 照片中获取 EXIF 数据,我希望获取 Date Taken 字段,然后将其翻转以读取 yyyy-MM-dd 这将成为文件夹的名称。下面列出了脚本源,以便在那里查看完整的脚本。
下面的脚本运行,但我得到日期格式为MM-dd-yyyy 的文件夹,我觉得我错过了一些真正简单的东西。任何帮助将不胜感激!
File date metadata not displaying properly 这个问题正是我观察到的行为,返回的日期时间字符串是 22 个字符,我尝试将 [char]8206 and [char]8207 替换为 '' 但返回错误:
使用“3”个参数调用“ParseExact”的异常:“字符串不是
被识别为有效的日期时间。”
$NewDateFormat = [日期时间]::ParseExact($DateTaken2, 'yyyy-MM-dd',$null)
#script source:
http://superwidgets.wordpress.com/category/powershell/
http://superwidgets.wordpress.com/2014/08/15/powershell-script-to-get-detailed-image-file-information-such-as-datetaken/
Sam Boutros 的脚本
v1.0 - 2015 年 1 月 11 日
$Images | ForEach-Object { $DateTaken = $_.DateTaken.Split(' ')[0].Replace('/','-')
$DateTaken2 = ($DateTaken -replace [char]8206) -Replace([char]8207)
$NewDateFormat = [datetime]::ParseExact($DateTaken2, 'yyyy-MM-dd',$null)
IF (-not (Test-Path $Source\$DateTaken2)){"Create $Source\$DateTaken2"
New-Item -Path "$Source\$DateTaken2" -ItemType Directory -Confirm:$false}
Move-Item -Path $_.FullName -Destination "$Source\$DateTaken2" -Confirm:$false
}
【问题讨论】:
-
$_.DateTaken出错时究竟包含什么? IMO,您最好使用正则表达式将日期元素过滤为仅数字,只要顺序正确不成问题。 -
我在几个步骤中添加了
> file.log。在替换之前 / 使用 - 然后在替换之后和替换 [char]8206 和 8207 之后。这是内容:12/17/2017 10:31 AM12-17-2017 12-17- 2017 -
因为
get-date ('12/17/2017 10:31 AM' -replace [char]8206 -Replace [char]8207)从上面的字符串返回了一个正确的日期,所以苏打柳的答案看起来很有希望
标签: string powershell datetime metadata