by Christian Fredh
9. November 2009 04:46
I had a problem. I had all these pictures taken from my recent journeys. When copying them over to my computer, they all had obscure file names like IMG_343.NEF. They were in raw format (.NEF) so the otherwise nice Windows feature of importing pictures didn’t recognize them, and the camera program didn’t have a fast way renaming them.
So, I thought: Let’s try to solve it with PowerShell, felt that I needed to play around with PowerShell anyway. Thought I share the script I used. It could definitely be used with other files as well, but I think pictures is the most common files to apply it to.
Please, be aware that I’m not a PowerShell expert, I’m not responsible of what happens to your files, and take backup of your files before using it.

Get-ChildItem | Sort-Object CreationTime | %{$suffixNumber=1} { %{$suffixString="{0:D3}" -f $suffixNumber}
{ Rename-Item $_ -NewName "Las Vegas $suffixString.nef" ; $suffixNumber++ } }
The Get-ChildItem cmdlet gets all files in the folder you are in, and then we are piping the results to Sort-Object which sorts the files by creation date, and then for each file, we are renaming it and then increasing the number we are adding to the file name. I read the article Renaming Files by Jeffrey Snover to help me with this.
Change the $suffixNumber=1 if you want it to start from 0 or some other number. The $suffixString="{0:D3}" -f $suffixNumber is formatting the number to include leading zeros if nessesary. You can change the number 3 if you want more or less leading zeros. I read the article Windows PowerShell Tip of the Week - Formatting Numbers to help me with this. And, of course, change "Las Vegas " to what ever makes sense to your case.
The result was files with nice looking file names, Las Vegas 001.nef, Las Vegas 002.nef, Las Vegas 003.nef and so on.
Enjoy!