我以Media Created
这种方式从文件资源管理器中检索列字段:
$mCreated = $shellfolder.GetDetailsOf($shellfile, 208);
这很好用。它给了我字符串“5/7/2017 4:09 PM”
问题:
当我尝试将字符串转换为格式化日期时,出现此错误:
Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."
At C:\Client\testVideos\anotherTest.ps1:28 char:9
+ $formattedDate = [Datetime]::ParseExact("$mCreated".Trim(), ' ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : FormatException
我试过的:
分别尝试了这些,都给了我同样的错误:
$formattedDate = [Datetime]::ParseExact("$mCreated".Trim(), 'yyyyMMddTHHmmss', $null)
$formattedDate = [Datetime]::ParseExact("$mCreated".Trim(), 'yyyyMMddTHHmmss', [Globalization.CultureInfo]::InvariantCulture)
# This line actually gives me a different error
$formattedDate = Get-Date "$mCreated".Trim() -Format "yyyyMMddTHHmmss"
# ERROR: Get-Date : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input
什么有效:
我也一直Created Date
在使用相同的代码并以相同的方式对其进行格式化,并且它有效!
创建日期为“2/12/2021 1:10 PM”、“2/4/2021 3:39 PM”、“2/2/2021 9:00 AM”或“2/4/2021 3:54”下午”
# This formats it successfully with no error
$createdDate = $shellfolder.GetDetailsOf($shellfile, 4);
$formattedDate = $createdDate | Get-Date -Format "yyyyMMddTHHmmss"
相关但最终无济于事:
https://stackoverflow.com/questions/42784919/parseexact-string-was-not-recognized-as-a-valid-datetime https://stackoverflow.com/questions/48637312/convert-string-to-powershell-datetime
I had the same issue in the past with various dates returned by the
Folder.GetDetailsOf()
method. I found the issue to be Unicode formatting charactersMy solution was to "whitelist" valid DateTime characters in a regualr expression. So the regex matches for "good" characters are:
\w
Any word character (0..9, 'A', 'P', and 'M')\:
Colons (Time separator)/
Slashes (Date separator)These can be combined & enclosed in brackets to create a character class that will match valid characters.:
[\w\:/ ]
To invert the class, & match invalid characters, prepend a caret to the class definition:
[^\w\:/ ]
Then we can use the -Replace operator to remove them:
[DateTime]($MediaCreated -replace '[^\w\:/ ]','')
Just recently learned of the alternative:
ShellFolderItem.ExtendedProperty()
我找不到“Media Created”的正确名称,但 * FMTID/PID* 对是
"{2E4B640D-5019-46D8-8881-55414CC5CAA0}100"
:继续我的评论,顺便说一句,你也可以这样做来格式化。
将日期字符串转换为日期时间对象
详细信息和更多信息:
至于带有 parseexact 的字符串。它受系统日期设置方式的影响,并且 yeat 必须位于时间戳的开头或结尾。在此处查看详细信息:
或者你最终在这里:
在此处查看详细信息:
无法打印/不可见的字符是尝试使用调用中的日期字符串时的问题。
在常规 cmd.exe(不是来自 PowerShell (ISE/consolehost) 或通过 Windows 终端的 cmd.exe)中,Notepad/consolehost/ISE/VSCode/Windows 终端中没有出现无法打印/不可见的字符。
因此,为什么基思的正则表达式有效......
...因此我们可以更直接地获取它,以允许 parseexact 工作,而不必更改数据类型: