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]): 
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:


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:
Visit the Windows PowerShell ScriptCenter at: