我试图解决我的照片使用PowerShell的元数据。我需要设置EXIF DateTaken如文件创建时间
这里是我的代码:
[reflection.assembly]::loadfile( "C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Drawing.dll")
echo "--------"
echo ""
$files = Get-ChildItem E:\Photos\*.*
ForEach ($file in $files) {
#Get Date
$foo=New-Object -TypeName system.drawing.bitmap -ArgumentList $file.fullname
$date = $foo.GetPropertyItem(36867).value[0..9]
$arYear = [Char]$date[0],[Char]$date[1],[Char]$date[2],[Char]$date[3]
$arMonth = [Char]$date[5],[Char]$date[6]
$arDay = [Char]$date[8],[Char]$date[9]
$strYear = [String]::Join('',$arYear)
$strMonth = [String]::Join('',$arMonth)
$strDay = [String]::Join('',$arDay)
$DateTaken = $strDay + "/" + $strMonth + "/" + $strYear
# Get Time
$time = $foo.GetPropertyItem(36867).value[11..18]
$arHour = [Char]$time[0],[Char]$time[1]
$arMinute = [Char]$time[3],[Char]$time[4]
$arSecond = [Char]$time[6],[Char]$time[7]
$strHour = [String]::Join('',$arHour)
$strMinute = [String]::Join('',$arMinute)
$strSecond = [String]::Join('',$arSecond)
$TimeTaken = $strHour + ":" + $strMinute + ":" + $strSecond
# Link into one date and time
$FullDate = $DateTaken + " " + $TimeTaken
echo "File name"
echo $file.name
echo ""
echo "Wrong data"
echo (Get-Item $file).creationtime.datetime
echo ""
echo "Right data"
echo $FullDate
echo ""
#Set DateTaken as CreationTime
(Get-Item $file).creationtime= $FullDate
pause
}
我得到了错误“因为它正被另一个进程无法访问文件”。我不知道如何解决它。
PS。我不知道如果$FullDate
有正确的语法,因为我无法测试
谢谢
你试过获得通过调用$ foo.dispose()摆脱了$ foo的对象?
由于@TomG,这解决了我的问题,我已经完成了这个项目。我张贴的代码,这可能会帮助一个人在未来的照片固定日期
[reflection.assembly]::loadfile( "C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Drawing.dll")
Clear-Host
$files = Get-ChildItem E:\Photos\*.*
$i = 1
ForEach ($file in $files) {
# Print file name
echo $file.fullname
# Print Number of File
# Write-Host 'File no: ' $i
# echo ""
#Get Date
$foo=New-Object -TypeName system.drawing.bitmap -ArgumentList $file.fullname
$date = $foo.GetPropertyItem(36867).value[0..9]
$arYear = [Char]$date[0],[Char]$date[1],[Char]$date[2],[Char]$date[3]
$arMonth = [Char]$date[5],[Char]$date[6]
$arDay = [Char]$date[8],[Char]$date[9]
$strYear = [String]::Join('',$arYear)
$strMonth = [String]::Join('',$arMonth)
$strDay = [String]::Join('',$arDay)
# Date to string
# $DateTaken = $strDay + "/" + $strMonth + "/" + $strYear
# Get Time
$time = $foo.GetPropertyItem(36867).value[11..18]
$arHour = [Char]$time[0],[Char]$time[1]
$arMinute = [Char]$time[3],[Char]$time[4]
$arSecond = [Char]$time[6],[Char]$time[7]
$strHour = [String]::Join('',$arHour)
$strMinute = [String]::Join('',$arMinute)
$strSecond = [String]::Join('',$arSecond)
$foo.dispose()
# Time to string
# $TimeTaken = $strHour + ":" + $strMinute + ":" + $strSecond
$NewDate = (Get-Date -Year $strYear -Month $strMonth -Day $strDay -Hour $strHour -Minute $strMinute -Second $strSecond)
#Set DateTaken as CreationTime
(Get-Item $file).CreationTime = $NewDate
(Get-Item $file).LastWriteTime = $NewDate
$i++
# Clear-Host
}
echo ""
echo "Done"
pause
从Flickr传送我的图片我有同样的问题......上述和其他一些在线资源的帮助下,我想出了
# ==============================================================================================
#
# UPDATE: Sam Ellis - @elliz
# DATE: 08 February 2018
# COMMENT: Added dispose, simplified date extraction, added error cases and tests
# COMMENT: Also edits file create and update date based on Image Taken EXIF data
# COMMENT: Added progress
#
# UPDATED: Steve Smith - @ardalis
# DATE: 18 January 2009
# COMMENT: Changed file paths and confirmed it works. Note that file extension must be .psONE not .psELL
#
# AUTHOR: Kim Oppalfens,
# DATE : 12/2/2007
#
# COMMENT: Helps you organise your digital photos into subdirectory, based on the Exif data
# found inside the picture. Based on the date picture taken property the pictures will be organized into
# [directory script called from]\YYYY\YYYY-MM-DD
# ==============================================================================================
Add-Type -AssemblyName System.Drawing
$imageSourcePath = Get-Location
$files = Get-ChildItem -Path $imageSourcePath -filter *.jpg # -recurse
$numFiles = $files.Count
[Int32]$counter = 0
[Int32]$takenDatePropertyId = 36867
foreach ($file in $files) {
$takenDate = $Null
$counter++
Write-Progress "$numFiles image files." -PercentComplete (100 * $counter / $numFiles)
$image = New-Object -TypeName System.Drawing.Bitmap -ArgumentList $file.FullName
[string]$targetPath = $Null
try {
$takenDateExists = $image.PropertyIdList -Contains $takenDatePropertyId
if (-Not $takenDateExists) {
Write-Warning "File '$( $file.Name )' has no taken date EXIF. Moving to 'Unknown' folder."
$targetPath = Join-Path $imageSourcePath "Unknown"
}
Else {
$dateCharArray = $image.GetPropertyItem(36867).Value[0..18] # Omitting last null character from array
[String]$dateString = [System.Text.Encoding]::ASCII.GetString($dateCharArray)
$takenDate = [DateTime]::ParseExact($dateString, 'yyyy:MM:dd HH:mm:ss', $Null)
$targetPath = Join-Path $imageSourcePath "$( Get-Date $takenDate -Format yyyy )\$( Get-Date $takenDate -Format yyyy-MM-dd )"
}
$image.Dispose()
If (-Not (Test-Path $TargetPath)) {
New-Item $TargetPath -Type Directory | Out-Null
}
# Change create date to taken date
if ($Null -ne $takenDate) {
$file.CreationTime = $takenDate
}
Move-Item -Path $file.FullName -Destination $targetPath | Out-Null
# xcopy /Y/Q $file.FullName $TargetPath | Out-Null # this creates new item with new create date
}
catch {
$err = $_
Write-Error "Error processing image $( $file.Name ):/r/n$err"
if ($Null -ne $image) {
$image.Dispose()
}
}
}
Write-Output "Finished moving $numFiles files from $imageSourcePath"
触发从管理员的PowerShell外壳脚本,而在你的照片,享受的目录。