无法从JPG文件的DateTaken属性将字符串转换为整数变量

时间:2018-10-01 14:15:52

标签: powershell

我有一个脚本,该脚本正在使用JPG文件中的EXIF数据来查找DateTaken值。找到后,将数据放在$Year$Month变量中。

$objShell  = New-Object -ComObject Shell.Application
$folders = (Get-ChildItem G:\ServerFolders\Photos\UnSorted\ -Directory -Recurse -force).FullName

foreach ($Folder in $folders) {
    $objfolder = $objShell.Namespace($folder)

    foreach ($file in $objFolder.Items()) {
        if ($objfolder.GetDetailsOf($file, 156) -eq ".jpg") {
            $yeartaken = ($objFolder.GetDetailsOf($File, 12)).Split("/")[2].Split(" ")[0]
            $month = $objFolder.GetDetailsOf($File, 12).Split("/")[1]
            $monthname = (Get-Culture).DateTimeFormat.GetMonthName($month)

            Write-Host $file.Name
        }
    }
}

因此,如果文件的DateTaken为06/10/2016,则$yeartaken2016,而$month10

然后我到Get-Culture将10转换为10月。这不起作用,因为它将$month视为字符串。

Cannot convert argument "month", with value: "‎10", for "GetMonthName" to
type "System.Int32": "Cannot convert value "‎10" to type "System.Int32".
Error: "Input string was not in a correct format.""
At line:1 char:3
+   (Get-Culture).DateTimeFormat.GetMonthName($month)
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

我尝试使用强制转换或转换将其转换为Integer值,但是由于某些原因,它无法转换。

PS> [int]$Test = $month

Cannot convert value "‎10" to type "System.Int32". Error: "Input string was
not in a correct format."
At line:1 char:1
+ [int]$Test = $month
+ ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : MetadataError: (:) [], ArgumentTransformationMetadataException
    + FullyQualifiedErrorId : RuntimeException

2 个答案:

答案 0 :(得分:1)

使用日期的一种更好的方法是将日期字符串转换为实际的DateTime对象,该对象提供了您正在寻找的所有信息:

$culture = [Globalization.CultureInfo]::InvariantCulture
$pattern = 'dd\/MM\/yyyy'

$datestring = $objFolder.GetDetailsOf($File, 12).Split(' ')[0]
$datetaken  = [DateTime]::ParseExact($datestring, $pattern, $culture)

$year      = $datetaken.Year
$month     = $datetaken.Month              # month (numeric)
$monthname = $datetaken.ToString('MMMM')   # month name

假设日期后跟一个HH:mm:ss格式的时间,则可以扩展代码来处理时间:

$culture = [Globalization.CultureInfo]::InvariantCulture
$pattern = 'dd\/MM\/yyyy HH:mm:ss'

$datestring = $objFolder.GetDetailsOf($File, 12)
$timestamp  = [DateTime]::ParseExact($datestring, $pattern, $culture)

$year      = $timestamp.Year
$month     = $timestamp.Month              # month (numeric)
$monthname = $timestamp.ToString('MMMM')   # month name
$hour      = $timestamp.Hour
...

答案 1 :(得分:1)

$objFolder.GetDetailsOf($File, 12)返回的日期和时间字符串包含不可见的控制字符 [1] ,原因不明。我。

您可以按如下所述将其剥离,然后代码将起作用:

# Remove the formatting control characters from the string, by replacing
# all instances of Unicode character category \p{Cf} with the empty string.
# For more info on Unicode categories, see http://www.regular-expressions.info/unicode.html
$dateTimeStr = $objFolder.GetDetailsOf($File, 12) -replace '\p{Cf}'

[int] $yeartaken = $dateTimeStr.Split("/")[2].Split(" ")[0]
[int] $month = $dateTimeStr.Split("/")[1]
$monthname = (Get-Culture).DateTimeFormat.GetMonthName($month)

顺便提一句,一般来说,Ansgar Wiecher's answer显示了更强大的日期时间字符串解析技术。


[1]在测试文件的元数据中,我发现了Unicode控制字符U+200E (LEFT-TO-RIGHT MARK)U+200F (RIGHT-TO-LEFT MARK) 的实例。