无法将字符串从 JPG 文件上的 DateTaken 属性转换为整数变量 [英] Unable to convert a string to an integer variable from DateTaken attribute on a JPG file
问题描述
我有一个脚本,它使用 JPG 文件中的 EXIF 数据来查找 DateTaken
值.找到后,将数据放在 $Year
和 $Month
变量中.
$objShell = New-Object -ComObject Shell.Application$folders = (Get-ChildItem G:\ServerFolders\Photos\UnSorted\ -Directory -Recurse -force).FullNameforeach($文件夹中的$文件夹){$objfolder = $objShell.Namespace($folder)foreach ($objFolder.Items() 中的 $file) {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)写主机 $file.Name}}}
因此,如果文件的 DateTaken 为 06/10/2016,$yeartaken
为 2016
,$month
为 10
然后我使用 Get-Culture
将 10 转换为 10 月.这不起作用,因为它将 $month
视为字符串.
我尝试使用强制转换或转换将其转换为整数值,但由于某种原因它无法转换.
<前>PS> [int]$Test = $month无法将值"10"转换为类型System.Int32".错误:输入字符串是格式不正确."在行:1 字符:1+ [int]$Test = $month+ ~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo : MetadataError: (:) [], ArgumentTransformationMetadataException+ FullQualifiedErrorId:运行时异常处理日期的更好方法是将日期字符串转换为实际的 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 # 月(数字)$monthname = $datetaken.ToString('MMMM') # 月份名称
假设日期后跟 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 # 月(数字)$monthname = $timestamp.ToString('MMMM') # 月份名称$hour = $timestamp.Hour...
I have a script which is using the EXIF data from a JPG file to find the DateTaken
value. Once found, place the data in $Year
and $Month
variables.
$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
}
}
}
So, if the file has a DateTaken as 06/10/2016, $yeartaken
is 2016
and $month
is 10
I then to Get-Culture
to convert the 10 into October. This doesn't work because it's seeing $month
as a string.
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
I've tried to convert it to a Integer value using casting or converting, but for some reason it won't convert.
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
A better approach to working with dates is to convert the date string to an actual DateTime
object, which provides all the information you're looking for:
$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
Assuming that the date is followed by a time in the format HH:mm:ss
you could extend the code to handle the time as well:
$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
...
这篇关于无法将字符串从 JPG 文件上的 DateTaken 属性转换为整数变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!