Udostępnij za pośrednictwem


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.

Comments

  • Anonymous
    January 01, 2003
    Ian, that would solve it, but it's not on any of my cameras. And I need to know I was how I was going to name the picture and set the camera before shooting. So I have to fix it afterwards.

  • Anonymous
    January 01, 2003
    Christian, Glad you liked the example. I do use vista and I do use tagging :-)  However there are 3 problems. (a) I would have 4 pictures called IMGP1234 (b) When files are archived to CD, fetching the tags is slow compared with fetching the file names. (c) When I do stack by tags I'd have extra tags that I don't want. Those "AB" ones are from an airshow in Abingdon, and they'll be tagged "Aircraft", and titled with things like "Red Arrows at Abingdon"  

  • Anonymous
    June 07, 2007
    you do not even need the foreach : dir | ren -new {$_.name.replace("IMGP","AB2")} Greetings //o//

  • Anonymous
    June 08, 2007
    Even with the foreach it meets my definition of a good script - it works, it does a useful job in an acceptable time frame and you can understand it. The danger with going too concise is that it is difficult for others to understand and therefore slows adoption of PowerShell as it is seen as being too cryptic.

  • Anonymous
    June 08, 2007
    Isn't there a setting on the camera where you can change the filename prefix. Or is that too easy?

  • Anonymous
    June 08, 2007
    You could also use Vista. When you import the pictures you get to set a tag which is essentially equal to "AB2" in your example. But that is besides the point. Your point is about how PS stack up against VB and I think your example show it very well.