Function Set-DateTaken
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true)][String]$Directory,
[Parameter(Mandatory=$true)][ValidateSet("Seconds","Minutes","Hours","Days")][String]$TimeType,
[int]$Amount=0
)
[System.Reflection.Assembly]::LoadWithPartialName("PresentationCore")
[System.Reflection.Assembly]::LoadWithPartialName("WindowsBase")
[System.Reflection.Assembly]::LoadWithPartialName("System.Xaml")
If(-not(Test-Path -Path $Directory))
{
Write-Verbose "$Directory was not found, exiting..."
exit
}
Else
{
Write-Verbose "$Directory found, proceeding..."
}
Write-Verbose "Grabing all image files from $Directory..."
$Files = Get-ChildItem -Path $Directory -Recurse -Include "*.jpg","*.jpeg","*.bmp","*.dib","*.gif","*.tff","*.tif","*.wmp","*.png"
Foreach($File in $Files)
{
$SourcePath = $File.FullName
Write-Verbose "$($File.FullName) source path set to $SourcePath"
$DestinationPath = $File.FullName.Replace($File.Extension,"-new$($File.Extension)")
Write-Verbose "$($File.FullName) destination path set to $DestinationPath"
[System.Windows.Media.Imaging.BitmapFrame]$Frame = $null
Write-Verbose "Opeing IO stream to $File.Name for reading"
$InputStream = [System.IO.File]::Open($SourcePath,[System.IO.FileMode]::Open,[System.IO.FileAccess]::Read)
If($File.Extension -match "\.jp(|e)g")
{
Write-Verbose "Decoder is set to JPG based on file extension"
$Decode = [System.Windows.Media.Imaging.JpegBitmapDecoder]::Create($InputStream,[System.Windows.Media.Imaging.BitmapCreateOptions]::PreservePixelFormat,[System.Windows.Media.Imaging.BitmapCacheOption]::None)
}
ElseIf($File.Extension -match "(\.bmp|\.dib)")
{
Write-Verbose "Decoder is set to BMP based on file extension"
$Decode = [System.Windows.Media.Imaging.BitmapDecoder]::Create($InputStream,[System.Windows.Media.Imaging.BitmapCreateOptions]::PreservePixelFormat,[System.Windows.Media.Imaging.BitmapCacheOption]::None)
}
ElseIf($File.Extension -match "(\.tiff|\.tif)")
{
Write-Verbose "Decoder is set to TIFF based on file extension"
$Decode = [System.Windows.Media.Imaging.TiffBitmapDecoder]::Create($InputStream,[System.Windows.Media.Imaging.BitmapCreateOptions]::PreservePixelFormat,[System.Windows.Media.Imaging.BitmapCacheOption]::None)
}
ElseIf($File.Extension -match "\.wmp")
{
Write-Verbose "Decoder is set to WMP based on file extension"
$Decode = [System.Windows.Media.Imaging.WmpBitmapDecoder]::Create($InputStream,[System.Windows.Media.Imaging.BitmapCreateOptions]::PreservePixelFormat,[System.Windows.Media.Imaging.BitmapCacheOption]::None)
}
ElseIf($File.Extension -match "\.png")
{
Write-Verbose "Decoder is set to PNG based on file extension"
$Decode = [System.Windows.Media.Imaging.PNGBitmapDecoder]::Create($InputStream,[System.Windows.Media.Imaging.BitmapCreateOptions]::PreservePixelFormat,[System.Windows.Media.Imaging.BitmapCacheOption]::None)
}
ElseIf($File.Extension -match "\.gif")
{
Write-Verbose "Decoder is set to GIF based on file extension"
$Decode = [System.Windows.Media.Imaging.GIFBitmapDecoder]::Create($InputStream,[System.Windows.Media.Imaging.BitmapCreateOptions]::PreservePixelFormat,[System.Windows.Media.Imaging.BitmapCacheOption]::None)
}
$Frame = $Decode.Frames[0]
$MetaData = $Frame.Metadata.Clone()
<#
Add new parameters in appropriate format for each meta dat tag
Research all meta data fields
Manipulate each meta data tag as needed
#>
$DateTaken = $MetaData.DateTaken
If($DateTaken -eq $null)
{
$DateTaken = $File.CreationTime.ToString()
}
$NewDate = [DateTime]$DateTaken
Switch ($TimeType)
{
"Seconds" { $NewDataTaken = $NewDate.AddSeconds($Amount); Break }
"Minutes" { $NewDataTaken = $NewDate.AddMinutes($Amount); Break }
"Hours" { $NewDataTaken = $NewDate.AddHours($Amount); Break }
"Days" { $NewDataTaken = $NewDate.AddDays($Amount); Break }
}
$MetaData.DateTaken = $NewDataTaken
Write-Verbose "Date Taken is now $NewDataTaken"
If($File.Extension -match "\.jp(|e)g")
{
Write-Verbose "Encoder is set to JPG based on file extension"
$Encoder = New-Object System.Windows.Media.Imaging.JpegBitmapEncoder
}
ElseIf($File.Extension -match "(\.bmp|\.dib)")
{
Write-Verbose "Encoder is set to BMP based on file extension"
$Encoder = New-Object System.Windows.Media.Imaging.BitmapEncoder
}
ElseIf($File.Extension -match "(\.tiff|\.tif)")
{
Write-Verbose "Encoder is set to TIFF based on file extension"
$Encoder = New-Object System.Windows.Media.Imaging.TiffBitmapEncoder
}
ElseIf($File.Extension -match "\.wmp")
{
Write-Verbose "Encoder is set to WMP based on file extension"
$Encoder = New-Object System.Windows.Media.Imaging.WmpBitmapEncoder
}
ElseIf($File.Extension -match "\.png")
{
Write-Verbose "Encoder is set to PNG based on file extension"
$Encoder = New-Object System.Windows.Media.Imaging.PngBitmapEncoder
}
ElseIf($File.Extension -match "\.gif")
{
Write-Verbose "Encoder is set to GIF based on file extension"
$Encoder = New-Object System.Windows.Media.Imaging.GifBitmapEncoder
}
Write-Verbose "Encoding new image file for $($File.Name)"
$Encoder.Frames.Add([System.Windows.Media.Imaging.BitmapFrame]::Create($Frame,$Frame.Thumbnail,$MetaData,$Frame.ColorContexts))
Write-Verbose "Creating new file for $($File.Name)"
$OutputStream = [System.IO.File]::Open($DestinationPath,[System.IO.FileMode]::Create,[System.IO.FileAccess]::ReadWrite)
Write-Verbose "Saving new file data for $($File.Name)"
$Encoder.Save($OutputStream)
Write-Verbose "Cleaing up streams and old file for $($File.Name)"
$InputStream.Close()
$OutputStream.Close()
Remove-item $SourcePath
Rename-Item $DestinationPath -NewName $SourcePath
}
}