{"id":64743,"date":"2007-06-01T23:26:00","date_gmt":"2007-06-01T23:26:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/06\/01\/how-can-i-change-the-file-extension-for-all-the-files-in-a-folder\/"},"modified":"2007-06-01T23:26:00","modified_gmt":"2007-06-01T23:26:00","slug":"how-can-i-change-the-file-extension-for-all-the-files-in-a-folder","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-change-the-file-extension-for-all-the-files-in-a-folder\/","title":{"rendered":"How Can I Change the File Extension for All the Files in a Folder?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! We backed up a whole bunch of files in a folder; when these files were backed up the word &#8220;old&#8221; was inserted as part of the file extension (e.g., Filename.exe became Filename.<B>old<\/B>exe). How can I restore these files to their original names and extensions?<BR><BR>&#8212; RP <\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, RP. Today is Friday, June 1<SUP>st<\/SUP>, which can only mean one thing: the Scripting Guys are rushing around trying to tie up loose ends before they pack their bags and head for <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/teched07\/default.mspx\"><B>TechEd 2007<\/B><\/A>. If you\u2019re going to be at TechEd we\u2019d be tickled pink if you\u2019d drop by the CMP Media Booth (booth 1301) in the Partners Expo Hall and say hi. And because we know that no one would ever say hi to the Scripting Guys without getting something in return, well, we\u2019ll be giving away copies of <I>Dr. Scripto\u2019s Fun Book<\/I> and give you a chance to win a <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/games07\/bobble.mspx\"><B>Dr. Scripto bobblehead doll<\/B><\/A> to boot. <\/P>\n<TABLE class=\"dataTable\" id=\"ERD\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. OK, fine. Technically, you don\u2019t even have to say hi: just swing by, grab the fun book, fill out the card for the drawing, and then you can leave. You don\u2019t even need to make eye contact.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Now, we realize not everyone is going to TechEd. So does that mean that the moment the Scripting Guys set foot in Orlando they\u2019ll forget all about the people who are <I>not<\/I> enjoying the Florida sunshine? Probably. But don\u2019t despair: we\u2019ve prepared a whole bunch of cool new stuff to keep you entertained (and informed) while we\u2019re out riding amusement park rides. In addition to your daily dose of <I>Hey, Scripting Guy!<\/I> we\u2019ll also have some new Windows PowerShell information; we\u2019ll have a two-part article on burning CDs in Windows Vista; and we\u2019ll even have an opportunity for you non-TechEd types to win a Dr. Scripto bobblehead. In other words, the Script Center isn\u2019t closing just because the Scripting Guys will be out-of-town for a week; check the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/default.mspx\"><B>home page<\/B><\/A> on Monday for more details.<\/P>\n<TABLE class=\"dataTable\" id=\"EKE\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. Oh, and if you could pick up our mail and water our plants while we\u2019re gone we\u2019d really appreciate it. Thanks!<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>In the meantime, one of those loose ends we need to tie up is today\u2019s column. And there\u2019s only one way to do that: come up with a script that can remove the word <I>old<\/I> from the file extension of all the files in a folder. You know, a script like this one:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colFiles = objWMIService.ExecQuery _\n    (&#8220;ASSOCIATORS OF {Win32_Directory.Name=&#8217;C:\\Test&#8217;} Where &#8221; _\n        &amp; &#8220;ResultClass = CIM_DataFile&#8221;)<\/p>\n<p>For Each objFile In colFiles\n    strExtension = objFile.Extension \n    strExtension = Replace(strExtension, &#8220;old&#8221;, &#8220;&#8221;)\n    strNewName = objFile.Drive &amp; objFile.Path &amp; objFile.FileName &amp; &#8220;.&#8221; &amp; strExtension\n    errResult = objFile.Rename(strNewName)\nNext\n<\/PRE>\n<P>Explain how the script works? No, we can\u2019t do that this time around; after all, we <I>do<\/I> have a plane to catch. Sorry.<\/P>\n<P>Oh; right. The Scripting Editor has \u2026 helpfully \u2026 reminded us that this is the morning of Friday, June 1<SUP>st<\/SUP> and the Scripting Guy who writes this column doesn\u2019t actually leave until 10:50 PM on Sunday, June 3<SUP>rd<\/SUP>. So, what the heck; let\u2019s explain how this script works. <\/P>\n<P>To begin with, we connect to the WMI service on the local computer. And yes, this script <I>will<\/I> work just as well against a remote computer; all you have to do is assign the name of that remote machine to the variable strComputer. For example:<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-fs-01&#8221;\n<\/PRE>\n<P>After making the connection we then use an <B>Associators Of<\/B> query to return a collection of all the files (all instances of the <B>CIM_DataFile<\/B> class) found in the folder C:\\Test:<\/P><PRE class=\"codeSample\">Set colFiles = objWMIService.ExecQuery _\n    (&#8220;ASSOCIATORS OF {Win32_Directory.Name=&#8217;C:\\Test&#8217;} Where &#8221; _\n        &amp; &#8220;ResultClass = CIM_DataFile&#8221;)\n<\/PRE>\n<P>And yes, that\u2019s definitely an odd-looking line of code. For now, however, don\u2019t worry too much about it. Just replace <I>C:\\Test<\/I> with the name of <I>your<\/I> folder and everything will be fine.<\/P>\n<P>Our next step is to set up a For Each loop to loop through all the items in the collection (that is, through all the items in the folder). The first thing we do in this loop is grab the existing file extension (stored in the <B>Extension<\/B> property) and assign that value to a variable named strExtension:<\/P><PRE class=\"codeSample\">strExtension = objFile.Extension\n<\/PRE>\n<P>In WMI, the file extension does <I>not<\/I> include the period; that means that if our first file is named Filename.oldexe, then the variable strExtension will be equal to this: <I>oldexe<\/I>.<\/P>\n<P>That\u2019s fine, except we want to get rid of the word <I>old<\/I>. How do we do that? Like this:<\/P><PRE class=\"codeSample\">strExtension = Replace(strExtension, &#8220;old&#8221;, &#8220;&#8221;)\n<\/PRE>\n<P>All we\u2019re doing here is calling the <B>Replace<\/B> function and replacing <I>old<\/I> with nothing (\u201c\u201d). Guess what that makes strExtension equal to now? As always, you\u2019re way ahead of us:<\/P><PRE class=\"codeSample\">exe\n<\/PRE>\n<P>Now that we\u2019ve recovered the original file extension it\u2019s time to rename the file. When you rename a file using WMI you must pass the entire file path as the new file name; in other words, we have to say <B>C:\\Test\\Filename.exe<\/B> and not just <B>Filename.exe<\/B>. Because of that, our next line of code builds the complete file path for us:<\/P><PRE class=\"codeSample\">strNewName = objFile.Drive &amp; objFile.Path &amp; objFile.FileName &amp; &#8220;.&#8221; &amp; strExtension\n<\/PRE>\n<P>What we\u2019re doing here is using selected properties of the original file as well as our new file extension in order to construct the new file path. The <B>Drive<\/B> property, as you might expect, returns the drive where the file is stored. Thus:<\/P><PRE class=\"codeSample\">C:\n<\/PRE>\n<P>The <B>Path<\/B> property is a kind of oddball-little property that returns folder information <I>minus<\/I> the drive letter and the file name. In other words, if the path is C:\\Test\\Filename.exe then the Path property is equal to <B>\\Test\\<\/B>. Combined with the Drive property, that gives us a path (so far) equal to this:<\/P><PRE class=\"codeSample\">C:\\Test\\\n<\/PRE>\n<P>Next up is the <B>FileName<\/B> property, which is the name of the file <I>minus<\/I> the file extension (and, of course, minus the dot between the name and the extension). Thus:<\/P><PRE class=\"codeSample\">C:\\Test\\Filename\n<\/PRE>\n<P>And then last but surely not least, we tack on the dot and the file extension (the new and improved file extension, which is stored in the variable strExtension). That\u2019s going to give us a brand-new file path:<\/P><PRE class=\"codeSample\">C:\\Test\\Filename.exe\n<\/PRE>\n<P>The minute we have the new file path we can then call the <B>Rename<\/B> method and rename the file:<\/P><PRE class=\"codeSample\">errResult = objFile.Rename(strNewName)\n<\/PRE>\n<P>From there we loop around and repeat the process with the next file in the collection.<\/P>\n<P>So remember, if you\u2019re going to be in Orlando June 4 through June 8 be sure and stop by the convention center and say hi to the Scripting Guys. You might as well; after all, what else is there to do in a place like Orlando? To tell you the truth, we can\u2019t imagine you\u2019d be able to find anything in Orlando that compares to the excitement of meeting the Scripting Guys.<\/P>\n<P>What\u2019s that? Watching the grass grow? Well, OK, we\u2019ll have to give you that one. Watching paint dry? Well, we\u2019ll give you that one, too. Watching \u2013 OK, we get the idea. But come by and say hi anyway. After all, saying hi to the Scripting Guys <I>is<\/I> better than a poke in the eye with a sharp stick. <\/P>\n<P>Granted, not <I>much<\/I> better. But better.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! We backed up a whole bunch of files in a folder; when these files were backed up the word &#8220;old&#8221; was inserted as part of the file extension (e.g., Filename.exe became Filename.oldexe). How can I restore these files to their original names and extensions?&#8212; RP Hey, RP. Today is Friday, June 1st, [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[38,11,3,12,5],"class_list":["post-64743","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-folders","tag-scripting-guy","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! We backed up a whole bunch of files in a folder; when these files were backed up the word &#8220;old&#8221; was inserted as part of the file extension (e.g., Filename.exe became Filename.oldexe). How can I restore these files to their original names and extensions?&#8212; RP Hey, RP. Today is Friday, June 1st, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64743","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=64743"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64743\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=64743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64743"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}