Saturday, September 14, 2013

Renaming Photos with Powershell

As I've mentioned previously, my wife and I have twins. One name Lucy, the other Abigail. We tend to get a little confused who is who, when reviewing photos so my wife asked me to come up with a simple way of renaming photos on the fly.

She normally plugs her phone or camera in to her laptop and copies the photos to a specific directory on our main backup share. I created 4 folders within the initial directory titled 'Abi', 'Lucy','Abi & Lucy', 'Lucy & Abi' to denote who is the only twin in the photo or left to right who is in the photo. She then runs the following script I wrote to append the first letter of their name to the end of the photo:


Function RenameFile($path,$newExt){

     Get-ChildItem -path $path | 

     Foreach-Object { 

          $baseName = $_.name.remove($_.name.length - $_.extension.length)
          Rename-Item -Path $_.fullname -newname ($baseName + " - " + $newExt + $_.extension) 
     }
}

RenameFile -path "H:\Photos\Abigail & Lucy\Abi" -newExt "A"
RenameFile -path "H:\Photos\Abigail & Lucy\Lucy" -newExt "L"
RenameFile -path "H:\Photos\Abigail & Lucy\Abi & Lucy" -newExt "AL"
RenameFile -path "H:\Photos\Abigail & Lucy\Lucy & Abi" -newExt "LA"