{"id":5691,"date":"2008-07-19T08:35:00","date_gmt":"2008-07-19T08:35:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2008\/07\/19\/shift-fileindex\/"},"modified":"2019-02-18T13:13:06","modified_gmt":"2019-02-18T20:13:06","slug":"shift-fileindex","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/shift-fileindex\/","title":{"rendered":"Shift-FileIndex"},"content":{"rendered":"<p>Klaus Graefensteiner has a great blog entry <a class=\"\" href=\"http:\/\/www.tellingmachine.com\/post\/2008\/07\/Renaming-a-series-of-indexed-files-with-Powershell.aspx\">HERE<\/a> where he publishes a great function called Shift-FileIndex.&nbsp; He was scanning files which created names with indexes (e.g. Vacation_0021.jpg ).&nbsp; Being the real world &#8211; things went wrong and he had duplicated pages which he then deleted which then left holes in the numbering and so on.&nbsp; He is halfway through Bruce Payette&#8217;s <a class=\"\" href=\"http:\/\/www.manning.com\/payette\/\">PowerShell in Action<\/a> book and decided to&nbsp;get his money&#8217;s worth and solve this problem using&nbsp;PowerShell to great effect.<\/p>\n<p>It is a very nice script which takes good advantage of Regular Expressions.&nbsp; During my talk in Atlanta someone asked me for an example of using &#8220;Capture Groups&#8221; in Regular Expressions.&nbsp; Klaus&#8217; script uses then to very good effect so check out his script (line 21).&nbsp; The capture group itself is quite chewy (which is why I didn&#8217;t have an answer off the top of my head in Atlanta [I always need to start with an example]):&nbsp; <br \/><span class=\"str\">&nbsp;&nbsp;&nbsp; &#8216;^(?&lt;NAME&gt;.*?)(?&lt;INDEX&gt;\\d{4})(?&lt;EXT&gt;\\..*$)&#8217;<br \/>You might ask, &#8220;why would I put up with that mess?&#8221; and the answer is that syntactic headache allows you a ton of syntactic simplicy downstream:<br \/>&nbsp;&nbsp; $matches.NAME<\/span><\/p>\n<p><span class=\"str\"><\/span>&nbsp;<\/p>\n<p>Here is a sample usage of the function he wrote.<\/p>\n<p>dir $Home\\Desktop\\Scans | Shift-FileIndex -firstIndex 39 -lastIndex 40 -shiftBy 100<br \/>dir $Home\\Desktop\\Scans | Shift-FileIndex -firstIndex 41 -lastIndex 121 -shiftDown -shiftBy 2<br \/>dir $Home\\Desktop\\Scans | Shift-FileIndex -firstIndex 139 -lastIndex 140 -shiftDown -shiftBy 19<\/p>\n<pre class=\"alt\">Great work Klaus!<\/pre>\n<pre class=\"alt\">Jeffrey Snover [MSFT]<br \/>Windows Management Partner Architect<br \/>Visit the Windows PowerShell Team blog at:&nbsp;&nbsp;&nbsp; <a href=\"http:\/\/blogs.msdn.com\/PowerShell\">http:\/\/blogs.msdn.com\/PowerShell<\/a><br \/>Visit the Windows PowerShell ScriptCenter at:&nbsp; <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\">http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx<\/a><br \/><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Klaus Graefensteiner has a great blog entry HERE where he publishes a great function called Shift-FileIndex.&nbsp; He was scanning files which created names with indexes (e.g. Vacation_0021.jpg ).&nbsp; Being the real world &#8211; things went wrong and he had duplicated pages which he then deleted which then left holes in the numbering and so on.&nbsp; [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[311],"class_list":["post-5691","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-shift-fileindex"],"acf":[],"blog_post_summary":"<p>Klaus Graefensteiner has a great blog entry HERE where he publishes a great function called Shift-FileIndex.&nbsp; He was scanning files which created names with indexes (e.g. Vacation_0021.jpg ).&nbsp; Being the real world &#8211; things went wrong and he had duplicated pages which he then deleted which then left holes in the numbering and so on.&nbsp; [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/5691","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=5691"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/5691\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=5691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=5691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=5691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}