Trying to modify a script to auto organize by date modified

by RDabate at 2013-01-03 12:46:26

The script below auto organizes photo’s based on their Exif data, i.e., "Date the photo was taken". This script takes the files that has no Exif data, and puts it in a folder called, "Exceptions". This works great, but I’d like to add to the end of the script to move everything in the "Exceptions" folder an organize them by date in the same way, but by using the "Date Modified" of the file. Any suggestions?


#==============================================================================================
#
# AUTHOR: Me
#
# COMMENT: Helps you organize your digital photos into a directory structure, based on the Exif data
# found inside the picture. Based on the date picture taken property the pictures will be organized into
# c:\organizedfotos\YYYY\MM\
#
# ==============================================================================================

[reflection.assembly]::loadfile( "C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Drawing.dll")

$targetFolder = "C:\Test"
$exceptionFolder = "C:\Test\Exceptions"
# Make sure you create this folder yourself as the script won’t do that automatically

$Files = Get-ChildItem $targetFolder -recurse -include .jpg,.png,.bmp,.mov
# Mae sure you add any extenstions that are in the "Target Folder"
foreach ($file in $Files)
{

try
{
$foo=New-Object -TypeName system.drawing.bitmap -ArgumentList $file.fullname
write-host $file.fullname -foregroundcolor Blue

#each character represents an ascii code number 0-10 is date
#10th character is space separator between date and time
#48 = 0 49 = 1 50 = 2 51 = 3 52 = 4 53 = 5 54 = 6 55 = 7 56 = 8 57 = 9 58 = :
#date is in YYYY/MM/DD format
$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
$TargetPath = $targetFolder + $strYear + "" + $strMonth + "" + $strDay

If (Test-Path $TargetPath)
{
xcopy /Y/Q $file.FullName $TargetPath
}
Else
{
New-Item $TargetPath -Type Directory
xcopy /Y/Q $file.FullName $TargetPath
}
}
catch
{
$logtext = "`n" + $file.fullname + " FAILED"
Add-Content C:\Test\Log\imagecopyexceptions.txt $logtext
$logtext = $Error[$error.count-1]
Add-Content C:\Test\Log\gecopyexceptions.txt $logtext


xcopy /Y/Q $file.FullName $exceptionFolder

}
}
by DonJ at 2013-01-03 12:59:03
I’m not entirely sure what you mean by "organize." Can you look at viewtopic.php?f=2&t=987 and see if that offers any help?

I don’t know anything about EFIX data, so a lot of your script just doesn’t make any sense to me - sorry.
by nohandle at 2013-01-04 05:38:57
this is my idea on how to do it.just a prototype it needs adding some advanced parameter options like mandatory, value from pipeline, some error preventing etc.
Function Get-ExifDataFromFile
{
param ([System.IO.FileInfo]$InputObject)
{
#get the data you need and add them to the
#original object as new properties
#I use ExifCreationTime
}
}
Function Get-OrganizePhotoDestination
{
param (
[System.IO.FileInfo]$InputObject,
[string]$SuccessPath=default to your default photo folder,
[string]$FailPath=default to your default exception
)
process {
$workingObject = (Get-ExifDataFromFile -InputObject $_)
if (($WorkingObject).ExifCreationTime)
{
$date = $WorkingObject.ExifCreationTime
$PathBase = $SuccesPath
}
else
{
$date = $InputObject.CreationTime
$PathBase = $FailPath
}
#create the path using the $date $pathBase

#output the file object with the destination property
}
}
Get-ChildItem | Get-OrganizePhotoPath | Copy-Item
by RichardSiddaway at 2013-01-04 06:32:50
The "organization" of the folder concept is fairly meaningless because if you view the folder in File Explorer it defaults to organizing by name as does powershell. if you want to see the files listed by date modified then a sort will do that for you

Get-ChildItem -File | sort LastWriteTime

or

Get-ChildItem -File | sort LastWriteTime -Descending

depending on the order you want