Shift-FileIndex

Shift-FileIndex

  • Comments 1

Klaus Graefensteiner has a great blog entry HERE where he publishes a great function called Shift-FileIndex.  He was scanning files which created names with indexes (e.g. Vacation_0021.jpg ).  Being the real world - things went wrong and he had duplicated pages which he then deleted which then left holes in the numbering and so on.  He is halfway through Bruce Payette's PowerShell in Action book and decided to get his money's worth and solve this problem using PowerShell to great effect.

It is a very nice script which takes good advantage of Regular Expressions.  During my talk in Atlanta someone asked me for an example of using "Capture Groups" in Regular Expressions.  Klaus' script uses then to very good effect so check out his script (line 21).  The capture group itself is quite chewy (which is why I didn't have an answer off the top of my head in Atlanta [I always need to start with an example]): 
    '^(?<NAME>.*?)(?<INDEX>\d{4})(?<EXT>\..*$)'
You might ask, "why would I put up with that mess?" and the answer is that syntactic headache allows you a ton of syntactic simplicy downstream:
   $matches.NAME

 

Here is a sample usage of the function he wrote.

dir $Home\Desktop\Scans | Shift-FileIndex -firstIndex 39 -lastIndex 40 -shiftBy 100
dir $Home\Desktop\Scans | Shift-FileIndex -firstIndex 41 -lastIndex 121 -shiftDown -shiftBy 2
dir $Home\Desktop\Scans | Shift-FileIndex -firstIndex 139 -lastIndex 140 -shiftDown -shiftBy 19

Great work Klaus!
Jeffrey Snover [MSFT]
Windows Management Partner Architect
Visit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx
Leave a Comment
  • Please add 1 and 1 and type the answer here:
  • Post
  • Thanks Jeffrey,

    before I started reading Bruce Payette's book, I thought I would use Powershell to automate  tasks for our platform (http://us.wonderware.com/products/sysplatform) by creating a few cmdlets, but at the moment I am thinking more about writing cmdlets and a custom provider. I am contemplating about crowning this custom Powershell framework with a little Domain Specific Language.

    My goal is to present this technology during our Wonderworlds conference in October (http://open.wonderware.com/sites/wworld08NA/default.aspx).

    If I find time, then I am going to blog about this endeavor.

    Klaus

Page 1 of 1 (1 items)