So as some of you may or may not know. My wife is conducting a 365 picture project this year. What’s a 365 picture project? Essentially take a pic of you, or something in your life for each day of the year and compile it all together at the end of the year to sort of journal everything. It’s, as you can imagine, a lot of pictures being taken a day. I would say on average she has 5 to 10 pictures taken a day. Realistically speaking, some days it’s about 2, others it’s near 30 or more.
Ok, great, why are you telling me this?
Well, I’m glad you asked. She would spend a lot of time sorting, renaming, and organizing them. I’ve been telling her a long time “just automate it” which is something I tell her about almost everything she does that’s repetitive. Well finally after a long night or sorting through some 100+ photos she called me on it, and had me write her a script to rename all the files for her. I also had a chance to get to show her a bit of that “computer stuff” I do everyday at work. Also, it’s been a while since I’ve done a scripting post, so I’m using this. 🙂
I figured I would go with powershell since I would have (easier) access to the .NET System.IO.DirectoryInfo class. One of the things my wife loves is chronological accuracy, so pulling a timestamp from the image as part of its name seemed like a good idea. She already had a routine where she copied them from her camera to an appropriate folder so specific file information was the only real concern here so this was going to be a very simple script. Below is the code:
PARAM([string]$FOLDER)
$ErrorActionPreference = "silentlycontinue"
if($FOLDER -eq "")
{$FOLDER = Read-Host "Path to picture folder?"}
$LIST = Get-ChildItem "$FOLDER*" -Exclude *ps1 `
-Include *jpg,*tiff,*jpeg,*gif,*bmp,*txt
$X = 0
ForEach($OBJECT in $LIST)
{$EXTENSION = $OBJECT.ToString().Split("") | Select -Last 1
$EXTENSION = $EXTENSION.Split(".") | Select-Object -Last 1
$FILEINFO = New-Object System.IO.DirectoryInfo($OBJECT)
$NAME = $FILEINFO.LastWriteTime.GetDateTimeFormats() `
| Select-Object -Index 99
$NAME = "$($NAME) ($($X)).$($EXTENSION)"
Write-Output $NAME
Rename-Item -Path "$OBJECT" $NAME
$X = $X+1
}
So what’s happening?
First we run this script, either by launching it directly, or by launching it on a command line followed by the folder where our pictures reside. The folder path is our only variable here, if one isn’t entered at the start of execution, it will prompt the user for one.
Now the script will build an object list of everything inside the folder taking special care to exclude any potential powershell scripts in the directory to the $LIST variable. I also built it to specifically include image file types (I didn’t want to rename some random non-picture files she might be storing in the directory (well, and txt for my testing purposes)).
Now we define an integer value to $X so we can enumerate it for a counter during our ForEach loop, which we begin next.
For each file in $LIST we:
- Grab the extension for the file to variable $EXTENSION
- Retrieve the last write time of the file and store it as $NAME
- Set $NAME to $NAME + $X + $EXTENSION
- Write the final $NAME to console
- Rename the item to $NAME
- Enumerate $X by 1
- Loop
The outcome?
I showed her the way it renamed a series of text files I had placed in a test folder on my laptop. At first, I received a rather dismissive “Oh, that’s good babe” however after I sat down at her desk and showed her the bad boy in action renaming another folder of 100 or so images in seconds, the sly grin found it’s way across her face. The joy of automation. The kind you can only get when you see something so small perform such a mind numbing laborious task for you.
She’s not quite ready to learn scripting, but at least now her eyes are open to other possibilities for automated solutions in her everyday computing. And as a stay at home mother of 2, I’m more than willing to help her streamline all her recreational and productive time at the keyboard. She is after all, my number 1 customer ;).
9 Comments
Love doing batch renaming…. on my Mac. Great little program called NameMangler. Does a wonderfully, beautiful job without the need to get technical. But it can if you like that kind of stuff.
Yea, there are plenty of apps as well for this type thing in windws, the import wizard is even capable of doing it. A quick google search pulled this up as an alternative:
http://download.cnet.com/Batch-File-Renamer/3000-2094_4-11655280.htmlohttp://download.cnet.com/Batch-File-Renamer/3000-2094_4-11655280.html
However, where’s the fun in that, especially since this lets me access the files journaled properties and pull attribute information. I could functionize this and add it to my profile, etc. Scripting offers a quick non-compiled solution, and that’s more along the lines of what I’m highlighting in scripts. Not neccesarily the function being performed. As an aside, if I was on a mac, I’d use bash to do something similar.
Oh and one other thing. Scripting is as free as beer. Using a bash solution in OSX wont cost you anything but the initial time to script it.
NameMangler was free as well and only cost the time to download, which was much quicker than me trying to script anything. 🙂
That’s interesting, it has a 25 use limit then requires a license to do more than 5 files at a time according to their user agreement.
http://itunes.apple.com/app/name-mangler/id402434720?mt=12
Well, when I got the app a few years ago it was free. Glad I did.
Sadly, for those who will follow in your footsteps, this is not the case 🙂
One of my favorite things about jasc paintshop back in the day was it’s batch renamer. Also thought it was cool a graphical editor did file management lol, but realistically it was batch conversions that you could utilize as a renamer and standardization tool.
Thanks babe, I need all the help I can get 🙂
Always a pleasure 🙂