我知道我必须遗漏一些简单的东西,但我已经盯着它看了一段时间,似乎无法看到它。
我有以下powershell代码
.".\Get-FileMetaDataReturnObject.ps1"
$BaseDirectory = "C:\Users\me\Desktop\Photo Test"
$Folders = get-childitem -path $BaseDirectory -Directory
foreach ($folder in $folders)
{
Write-Host "Folder name:$($folder.Name)"
$picture = Get-ChildItem -Path "$BaseDirectory\$($folder.Name)\"
$picMetaData = Get-FileMetaData -folder "$BaseDirectory\$($folder.Name)\"
$picDate = $picMetaData | Select 'Date Taken'
$picDateTaken = $picDate[0].'Date taken'
Write-Host $picDateTaken
$dateTime = [datetime]::parseexact($picDateTaken, "M/d/yyyy h:mm tt", [System.Globalization.CultureInfo]::InvariantCulture)
$dateStr = $dateTime.ToString('yyyy-MM-dd')
Write-Host $dateStr
}
当我运行它时,我收到以下错误
Folder name:Deruta - Umbria, September 4, 2012
9/4/2012 4:12 PM
Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."
At C:\Users\me\Desktop\Picture Folder Rename\PhotoFolderRename.ps1:18 char:5
+ $dateTime = [datetime]::parseexact($picDateTaken, "M/d/yyyy h:mm ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : FormatException
找到元数据脚本here
我真的不确定我已经搞砸了日期时间解析,所以任何帮助都会受到赞赏。
答案 0 :(得分:3)
将"9/4/2012 4:12 PM".Length
(20)与"9/4/2012 4:12 PM".Length
(16)进行比较,向我们展示了一些可疑的东西,实际上这个字符串并不像它看起来那样;其中有U+200E LEFT-TO-RIGHT MARK
和U+200F RIGHT-TO-LEFT MARK
个控制字符,这些字符是不可见的(或不是,取决于您的控制台设置)。替换这些将使字符串可解析:
$picDateTaken = $picDateTaken -replace "\u200e|\u200f", ""
一般来说更换控制字符会更复杂,我不确定是否有更好/更通用的方法来获取不变格式的元数据,但只要你确切知道你是什么处理这个问题已经足够了。