Copy Pictures To Folders By Date Taken with Powershell

Posted by: Steven Smith, on 19 Jan 2009 | View original | Bookmarked: 0 time(s)

For about the millionth time, I was downloading the photos from my digital camera and organizing them into folders by date... by hand.  I've tried a few different tools to do this in the past and have always ended up going back to a manual process for one reason or another.  So this time I grew fed up (again) with the inanity of the process and decided I'd use PowerShell to accomplish the task, as it seems made for the job.

Of course I searched first for others who had done this - it's hardly a problem unique to me.  I pretty quickly found someone who not only had gotten it to work but had some pretty nice links to help for those new to PowerShell.  Hans of course also linked to the original author, Kim, and his post on organizing photos into folders by EXIF date taken.  A couple of other useful links:

Here's a link to my working script:

The only hitch I ran into was that it's been so long since I've done anything with PowerShell, I forgot what the extension for it was.  I thought maybe it was psl not ps1 (and the font used on the original post didn't distinguish between the two).  Pea-ess-ell sounds more like PowerShell than pea-ess-one but at any rate if you try to run a .psl file from PowerShell, you'll simply get an error like:

Program 'foo.psl' failed to execute: No application is associated with the specified file for this operation.

Easy fix - rename it to foo.ps1.

Here's my final version of the script.  I also made the paths use YYYY-MM-DD as these sort properly chronologically when windows sorts the folders by filename.

   1: # ==============================================================================================
   2: # 
   3: # Microsoft PowerShell Source File -- Created with SAPIEN Technologies PrimalScript 4.1
   4: # 
   5: # NAME: OrgPhotos.ps1
   6: # 
   7: # UPDATED: Steve Smith
   8: # DATE: 18 January 2009
   9: # COMMENT: Changed file paths and confirmed it works.  Note that file extension must be .psONE not .psELL
  10: #
  11: # AUTHOR:  Kim Oppalfens, 
  12: # DATE  : 12/2/2007
  13: # 
  14: # COMMENT: Helps you organise your digital photos into subdirectory, based on the Exif data 
  15: # found inside the picture. Based on the date picture taken property the pictures will be organized into
  16: # c:\RecentlyUploadedPhotos\YYYY\YYYY-MM-DD
  17: # ============================================================================================== 
  18:  
  19: [reflection.assembly]::loadfile( "C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Drawing.dll") 
  20:  
  21: $Files = Get-ChildItem -recurse -filter *.jpg
  22: foreach ($file in $Files) 
  23: {
  24:   $foo=New-Object -TypeName system.drawing.bitmap -ArgumentList $file.fullname 
  25:  
  26: #each character represents an ascii code number 0-10 is date 
  27: #10th character is space separator between date and time
  28: #48 = 0 49 = 1 50 = 2 51 = 3 52 = 4 53 = 5 54 = 6 55 = 7 56 = 8 57 = 9 58 = : 
  29: #date is in YYYY/MM/DD format
  30:   $date = $foo.GetPropertyItem(36867).value[0..9]
  31:   $arYear = [Char]$date[0],[Char]$date[1],[Char]$date[2],[Char]$date[3]
  32:   $arMonth = [Char]$date[5],[Char]$date[6]
  33:   $arDay = [Char]$date[8],[Char]$date[9]
  34:   $strYear = [String]::Join(",$arYear)
  35:   $strMonth = [String]::Join(",$arMonth) 
  36:   $strDay = [String]::Join(",$arDay)
  37:   $DateTaken = $strYear + "-" + $strMonth + "-" + $strDay
  38:   $TargetPath = "c:\RecentlyUploadedPhotos\" + $strYear + "\" + $DateTaken
  39: If (Test-Path $TargetPath)
  40:   {
  41:     xcopy /Y/Q $file.FullName $TargetPath
  42:   }
  43:   Else
  44:    {
  45:     New-Item $TargetPath -Type Directory
  46:     xcopy /Y/Q $file.FullName $TargetPath
  47:    }
  48: } 
  49:  

Category: .NET | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 2936 | Hits: 9

Similar Posts

  • Eschewing Date Types in our Database more
  • Postgresql - Day 2 more
  • LINQ Tip of the week: System.DateTime support more
  • SQL SERVER - Retrieve - Select Only Date Part From DateTime - Best Practice - Part 2 more
  • New Date Data Types in Microsoft SQL Server 2008 more
  • Watch out for Date Kind in JSON Deserialization more
  • SQL SERVER - Few Useful DateTime Functions to Find Specific Dates more
  • JSON and Date Embedding more
  • Copy-in, Copy-out and reference semantics more
  • Archive and Compress Data from SQL Server more

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD