gpt4 book ai didi

powershell - Powershell循环每个文件名仅运行一次文件,即使文件名存在多个扩展名也是如此

转载 作者:行者123 更新时间:2023-12-02 23:49:40 25 4
gpt4 key购买 nike

我将是第一个承认PowerShell不是我的强项的人,但是经过一个晚上的互联网探索之后,我整理了以下内容。最终目标是通过DateTaken以及大量的sidecar XMP文件来组织大量的图像。它可能不是最优雅的代码,但几乎可以正常工作。
我不知道的最后一个问题是,无论扩展名如何,foreach循环每个文件名仅执行一次。例如,仅处理DSC00001.arw,DSC00001.xmp, DSC00001.jpg。
朝正确方向的任何观点将不胜感激。
谢谢!

$Folders = (Get-ChildItem -Path F:\ -Recurse -Directory -Force).FullName

$objShell = New-Object -ComObject Shell.Application
$Folders | % {

$objFolder = $objShell.namespace($_)
foreach ($File in $objFolder.items()) {
if (($File | Split-Path -Extension) -in ".arw",".gif",".tiff",".jpg",".png",".nef") {
Write-Host $File.Name`t -NoNewline -ForegroundColor Green
try {
$DateTaken = ($objFolder.getDetailsOf($File,12) -replace [char]8206) -replace [char]8207
$DateTaken = [DateTime]::ParseExact($DateTaken, "g", $null)
$Year = $DateTaken.ToString('yyyy')
$Date = $DateTaken.ToString('yyyy-MM-dd')
Write-Host $Date`t -ForegroundColor Blue -NoNewline
}
catch {
$Year = 'Other'
$Date = 'Other'
Write-Host $Date`t -ForegroundColor DarkRed -NoNewline
}
finally {
$DatePath = (Join-Path (Join-Path F:\ $Year ) $Date)
}
write-Host $File.Path -> (Join-Path $DatePath ($File | Split-Path -Leaf))-NoNewline
#Move-Item $File.Path (Join-Path $DatePath ($File | Split-Path -Leaf))
Write-Host Completed`n -NoNewline

if (Test-Path (Join-Path ($File | Split-Path) (($File | Split-Path -LeafBase) + '.xmp'))) {
Write-Host XMP:`t -ForegroundColor Magenta -NoNewLine
Write-Host (Join-Path ($File | Split-Path) (($File | Split-Path -LeafBase) + '.xmp')) -> (Join-Path ($DatePath) (($File | Split-Path -LeafBase) + '.xmp'))
#Move-Item (Join-Path ($File | Split-Path) (($File | Split-Path -LeafBase) + '.xmp')) (Join-Path ($DatePath) (($File | Split-Path -LeafBase) + '.xmp'))
}
}else {
Write-Host $File.Name is not an image `n -NoNewline -ForegroundColor DarkYellow
}
}

}

最佳答案

我不确定$objFolder.items()为什么不真正返回所有预期文件,但是我建议使用PowerShell的内置文件系统提供程序来发现每个文件夹中的实际文件,然后使用$objFolder.ParseName()获取可以传递给GetDetailsOf()的引用:

$Folders = (Get-ChildItem -Path F:\ -Recurse -Directory -Force).FullName

$objShell = New-Object -ComObject Shell.Application
$Folders | % {

$objFolder = $objShell.NameSpace($_)

foreach ($FileInfo in Get-ChildItem -LiteralPath $_ -File -Force) { # <-- trick is right here, use Get-ChildItem to discover the files in the folder
if($FileInfo.Extension -in ".arw",".gif",".tiff",".jpg",".png",".nef"){
$File = $objFolder.ParseName($FileInfo.Name) # <-- here we resolve the corresponding file item with shell app

# ... resolve and parse dateTaken here, just like before

# *.xmp file path might be easier to construct like this
$xmpPath = $FileInfo.FullName -replace "$($FileInfo.Extension)`$",'.xmp'
if(Test-Path -LiteralPath $xmpPath){
# ...
}
}
}
}

关于powershell - Powershell循环每个文件名仅运行一次文件,即使文件名存在多个扩展名也是如此,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63837795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com