Skip to content

Instantly share code, notes, and snippets.

@matt2005
Last active July 28, 2017 20:40
Show Gist options
  • Select an option

  • Save matt2005/e111f825cc4dae1c1009b25a0002ecdd to your computer and use it in GitHub Desktop.

Select an option

Save matt2005/e111f825cc4dae1c1009b25a0002ecdd to your computer and use it in GitHub Desktop.
sort photos via powershell
$srcFolder = "$env:USERPROFILE\Desktop\testing\New folder"
$targetFolder = "$env:USERPROFILE\Desktop\testing\sorted"
$files = Get-ChildItem -Path $srcFolder -include *.* -Recurse
foreach ($file in $files){
try{
$path = $file.FullName
$shell = New-Object -COMObject Shell.Application
$folder = Split-Path -Path $path
$file1 = Split-Path -Path $path -Leaf
$shellfolder = $shell.Namespace($folder)
$shellfile = $shellfolder.ParseName($file1)
#0..287 | Foreach-Object { '{0} = {1}' -f $_, $shellfolder.GetDetailsOf($null, $_) }
#32 CameraMaker,#12 DateTaken,#30 CameraModel
$dateTaken = $shellfolder.GetDetailsOf($shellfile, 12)
if([string]::IsNullOrWhiteSpace($dateTaken)) {
$parseDate =[datetime]$file.LastWriteTime
}
else{
#http://stackoverflow.com/questions/25474023/file-date-metadata-not-displaying-properly
$dateTaken = ($dateTaken -replace [char]8206) -replace [char]8207
$parseDate =[datetime]::ParseExact($dateTaken,'g',$null)
}
$year = $parseDate.Year
$monthNr = '{0:MM}' -f $parseDate
$month = '{0:MMMM}' -f $parseDate
$Day = '{0:dd}' -f $parseDate
$fileName = '{0:yyyyMMdd-hhmmss}' -f $parseDate
$fileExtension = $file.Extension
$fileGuid = [GUID]::NewGuid()
$directory = $targetFolder + '\' + $year + '\' + "$monthNr - $month" + '\' + "$Day"
if (!(Test-Path -Path $Directory))
{
$null = New-Item -Path $directory -ItemType directory
}
$newFileName = "$fileName-$fileGuid$fileExtension"
$targetFile = "$directory\$newFileName"
Copy-Item -Path $file.FullName -Destination $targetFile
}
catch{
Write-Verbose -Message "Could not copy file $file"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment