【问题标题】:Shell.Application getDetailsOf jpg Exif DateTaken not valid DateTimeShell.Application getDetailsOf jpg Exif DateTaken 无效 DateTime
【发布时间】: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 AM‎12-‎17-‎2017 12-17- 2017
  • 因为get-date ('12/‎17/‎2017 ‏‎10:31 AM' -replace [char]8206 -Replace [char]8207) 从上面的字符串返回了一个正确的日期,所以苏打柳的答案看起来很有希望

标签: string powershell datetime metadata


【解决方案1】:

我会说你的逻辑问题是你给ParseExact 格式 您想要的,而不是您在元数据中拥有的格式。此方法旨在从字符串创建DateTime 对象(基于您提供的格式),而不是格式化DateTime 对象。

你可以试试这个(在一个有 500 张图片的文件夹上测试 - 删除 -WhatIf 以采取行动):

$folderPath = "C:\UnsortedPics"

$newRootFolderPath = "C:\SortedPics"

# create root folder if does not exist
New-Item $newRootFolderPath -ItemType Directory -Force -WhatIf | Out-Null

# create shell object
$shell = New-Object -ComObject Shell.Application

# create folder object
$folder = $shell.NameSpace($folderPath)

foreach ($file in $folder.Items()) {

    # get raw date from file metadata
    $rawDate = ($folder.GetDetailsOf($file, 12) -replace [char]8206) -replace [char]8207

    if ($rawDate) {
        try {
            # parse to date object
            $date = [DateTime]::ParseExact($rawDate, "g", $null)

            # you could also use this without try/catch:
            #$date = New-Object Datetime
            #$parseSuccess = [DateTime]::TryParseExact($rawDate, "g", (Get-Culture), [System.Globalization.DateTimeStyles]::None, [ref]$date)

            # get wanted format
            $dateString = Get-Date $date -Format "yyyy-MM-dd"

            # create path
            $newFolderPath = Join-Path $newRootFolderPath $dateString

            # create folder if does not exist
            New-Item $newFolderPath -ItemType Directory -Force -WhatIf | Out-Null

            # move file
            Move-Item $file.Path -Destination $newFolderPath -Confirm:$false -WhatIf
        } catch {
            # ParseExact failed (would also catch New-Item errors)
        }
    } else {
        # no value for "Date Taken" property
    }

}

使用这种方法,您不再需要 TechNet 脚本 :)。

【讨论】:

  • 找不到这个了!需要找空闲时间重新测试一下!
  • 最终将这个答案精简为一个紧凑的脚本,真是一种享受!谢谢!
猜你喜欢
  • 2012-05-16
  • 2012-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多