Shell.Application getDetailsOf jpg Exif DateTaken 无效的日期时间
Shell.Application getDetailsOf jpg Exif DateTaken not valid DateTime
我正在使用 powershell 从 Jpg 照片中获取 EXIF 数据,我希望获取“拍摄日期”字段,然后将其翻转为 yyyy-MM-dd
,这将成为文件夹的名称。下面列出了脚本源,完整的脚本请在此处查看。
下面的脚本起作用了,但是我得到了日期格式为 MM-dd-yyyy
的文件夹,我觉得我错过了一些非常简单的东西。如有任何帮助,我们将不胜感激!
这个问题 File date metadata not displaying properly 正是我观察到的行为,返回的日期时间字符串是 22 个字符,我尝试用 ''
替换 [char]8206 and [char]8207
但是 returns 一个错误:
Exception calling "ParseExact" with "3" argument(s): "String was not
recognized as a valid DateTime."
$NewDateFormat = [datetime]::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 - 1/11/2015
$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
}
我会说你逻辑中的问题是你给了 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 脚本 :)。
我正在使用 powershell 从 Jpg 照片中获取 EXIF 数据,我希望获取“拍摄日期”字段,然后将其翻转为 yyyy-MM-dd
,这将成为文件夹的名称。下面列出了脚本源,完整的脚本请在此处查看。
下面的脚本起作用了,但是我得到了日期格式为 MM-dd-yyyy
的文件夹,我觉得我错过了一些非常简单的东西。如有任何帮助,我们将不胜感激!
这个问题 File date metadata not displaying properly 正是我观察到的行为,返回的日期时间字符串是 22 个字符,我尝试用 ''
替换 [char]8206 and [char]8207
但是 returns 一个错误:
Exception calling "ParseExact" with "3" argument(s): "String was not
recognized as a valid DateTime."
$NewDateFormat = [datetime]::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 - 1/11/2015
$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
}
我会说你逻辑中的问题是你给了 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 脚本 :)。