Powershell vs VB script.
OK, OK I know there is nothing as annoying as a convert. But here's a real world problem, and how my old solution stacks up against PowerShell.
First the problem: there is a standard for digital cameras which says they old use 4 digits to identify the pictures. So my Pentax cameras create files with names like IMGP1234 (for IMaGe - Pentax). I like having a serial number but since I've shot something over 20,000 pictures the filenames aren't unique. So I've got a scheme for changing IMGP to:
- IMGo for pictures from my Optio Compact
- IMG0 or IMG1 for Pictures from my *ist-D SLR (now retired)
- IMG2 for pictures from my K-10 SLR
I also replace the IMG part for sets of pictures taken together. For example the picture on the right is IND13775+.JPG. IND tells me it was take in India, 13775 fits it into sequence with my *ist-D photos and the + tells me it has been edited (somewhere there is a direct from camera version without the +). Windows will rename my files on import, but that removes the serial number. So I have a .VBS script as follows
Set fso = CreateObject("Scripting.FileSystemObject")
set folder = fso.getFolder("C:\Dump\Unsorted Pictures\Abingdon Airshow")
for each file in folder.files
on error resume next
if lcase(left(file.name,4)) = "imgp" then
file.name = "AB2" & mid(file.name,5)
end if
next
set folder = nothing
set fso = nothing
So... I was looking at how to do this in Powershell
dir | foreach {ren $_.fullname $_.name.replace("IMGP", "AB2")}
That's it. ONE LINE. Not even a very complicated line. [Update thanks to the PowerShell guy for commenting that I could have made this even shorter - hey I still think "for each ...." have to ditch the paradigms of VB]
OK, yes, it doesn't cope with IMGP1234.JPG changing case to Imgp1234+.jpg. And yes I know that PowerShell purists will say that dir, foreach and ren are all aliases and that parameters should be named rather than passed by position - but the point wasn't to produce a supportable script here. I wanted to show how small it is but also we're not having to resort to anything difficult. We've got
- dir | something dir | find and dir | more have been staples for years.
- foreach . A for loop ! We've been doing those for even longer. for %f in (*.*) do something_with %f has been in DOS and Windows for years.
- ren fullpath newname. Again just like we've been doing since the 1980's .
- replace("oldText", "newText"). Might not been in batch scripting, but magic it ain't.
What's new here ? Using $_ instead of %f for "the current one" in a for loop. The output of dir being objects with .name and .fullname properties, and those properties having a replace method. Hmm. It makes me think
Error: Rocket Science not found.