下午好,Powershell巫师们!
我希望有人能向我解释我如何解决这个问题,更重要的是,问题到底是什么!
我试图修复我几年前写的一个旧脚本,该脚本在一个文件属性上搜索几个日期,并选择一个用于重命名该文件。
我遇到的问题是,当我使用parseExact时,从文件中读取的日期字符串会失败.但是,如果我手动在powershell中键入相同的字符串,它就能工作!
请注意,这个脚本只会在我的电脑上运行,只需要处理我的文件格式中的日期,所以我不太担心$null的使用,除非它是相关的。
见下面的例子:
Write-Host "TEST 1"
$DateTime = [DateTime]::ParseExact("240720211515","ddMMyyyyHHmm",$null)
Write-Host $DateTime # WORKS!
Write-Host "TEST 2"
$DateTime2 = [DateTime]::ParseExact("240720211515","ddMMyyyyHHmm",$null)
Write-Host $DateTime2 # FAILS!
看起来一样对吗?
下面是一个更真实的例子,说明我所做的事情都失败了
$file = Get-Item "C:\SomeFolder\somefile.jpg"
$shellObject = New-Object -ComObject Shell.Application
$directoryObject = $shellObject.NameSpace( $file.Directory.FullName )
$fileObject = $directoryObject.ParseName( $file.Name )
$property = 'Date taken'
for(
$index = 5;
$directoryObject.GetDetailsOf( $directoryObject.Items, $index ) -ne $property;
++$index) { }
$photoDate = $directoryObject.GetDetailsOf($fileObject, $index)
Write-Host $photoDate # <-- This reads 03/08/2021 09:15
$output = [DateTime]::ParseExact($photoDate,"dd/MM/yyyy HH:mm",$null) # <-- This fails
Write-Host $output
# If i manually type in here it works.... If I copy and paste from the Write-Host it fails...
$someInput = "03/08/2021 09:15"
$workingOutput = [DateTime]::ParseExact($someInput,"dd/MM/yyyy HH:mm",$null)
Write-Host $workingOutput
发布于 2022-12-03 12:00:26
当我开始使用PowerShell探索元数据时,我遇到了这个问题。我的解决方案是创建一个regex“倒排白名单”字符类,并删除(用‘’代替‘)所有非白名单字符。
但是,后来我了解到了一个可供FolderItem遵循的替代方法:ExtenedProperty()方法。
从项的属性集中获取属性值。可以通过名称或属性集的格式标识符(FMTID)和属性标识符(PID)指定该属性。
它的主要强度是fe转折点类型对应于属性值类型:
当此方法返回时,如果属性值存在于指定项,则包含该属性值。该值将具有完整的类型-例如,日期作为日期返回,而不是字符串。
使用此方法访问日期属性可以消除字符串解析和遇到的问题:
PS Pictures> $FileInfo = Get-Item "C:\Users\keith\Pictures\Leland\2009\Leland 191.JPG"
PS Pictures> $Shell = New-Object -ComObject shell.application
PS Pictures> $comFolder = $Shell.NameSpace($FileInfo.DirectoryName)
PS Pictures> $comFile = $comFolder.ParseName($FileInfo.Name)
PS Pictures>
PS Pictures> $comFolder.GetDetailsOf($null,12)
Date taken
PS Pictures> $comFolder.GetDetailsOf($comFile,12)
9/5/2009 2:06 PM
PS Pictures> $comFolder.GetDetailsOf($comFile,12).GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
PS Pictures> $comFile.ExtendedProperty("DateTaken")
PS Pictures> $comFile.ExtendedProperty("System.Photo.DateTaken")
Saturday, September 5, 2009 07:06:41 PM
PS Pictures> $comFile.ExtendedProperty("System.Photo.DateTaken").GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True DateTime System.ValueType
它还返回可以包含多个值的属性数组(标记、贡献艺术家等/):
PS Pictures> $comFolder.GetDetailsOf($null,18)
Tags
PS Pictures> $comFolder.GetDetailsOf($comFile,18)
Leland; Tim; Jorge
PS Pictures> $comFolder.GetDetailsOf($comFile,18).GetTYpe()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
PS Pictures> $comFile.ExtendedProperty("System.KeyWords")
PS Pictures> $comFile.ExtendedProperty("System.Keywords")
Leland
Tim
Jorge
PS Pictures> $comFile.ExtendedProperty("System.Keywords").GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String[] System.Array
但是GetDetailsOf()
仍然很有用,特别是对于使用EnumList的值:
PS Pictures> $comFolder.GetDetailsOf($null,261)
Flash mode
PS Pictures> $comFolder.GetDetailsOf($comFile,261)
No flash, auto
PS Pictures> $comFile.ExtendedProperty("System.Photo.Flash")
24
https://stackoverflow.com/questions/74617304
复制相似问题