{"id":68453,"date":"2005-11-28T15:47:00","date_gmt":"2005-11-28T15:47:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/11\/28\/how-can-i-rename-files-like-_jkg1234-jpg-to-272_dsc_1234-jpg\/"},"modified":"2005-11-28T15:47:00","modified_gmt":"2005-11-28T15:47:00","slug":"how-can-i-rename-files-like-_jkg1234-jpg-to-272_dsc_1234-jpg","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-rename-files-like-_jkg1234-jpg-to-272_dsc_1234-jpg\/","title":{"rendered":"How Can I Rename Files Like _JKG1234.jpg to 272_DSC_1234.jpg?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I rename files like _JKG1234.jpg to 272_DSC_1234.jpg?<BR><BR>&#8212; NA<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, NA. You know, NA, we feel sorry for you. Why? Well, on the one hand you\u2019re unbelievably lucky. After all, the Scripting Guys never write scripts that they actually use, yet you somehow managed to hit upon an exception. Talk about defying the odds! At the same time, however, you\u2019re unbelievably <I>unlucky<\/I>: after all, the forces of the cosmos were perfectly aligned for you, yet you used up all that good karma on sending a question to the Scripting Guys instead buying yourself a lottery ticket. Ouch!<\/P>\n<P>Two years ago one of the Scripting Guys went to Japan to watch his son play baseball. (Let\u2019s face it, there\u2019s a baseball angle to almost <I>everything<\/I> this Scripting Guy does.) While in Japan our Scripting Guy took a bunch of digital photos, photos that were automatically assigned names like 100_1023.jpg and 100_1024.jpg. This Scripting Guy was way too lazy to rename each and every photo, but he did think it might be useful if he at least modified the name to make it clear that the picture was part of the Japanese baseball collection. In other words, he wanted to take a photo named 100_1023.jpg and rename it Japan_1023.jpg. And, believe it or not, he <I>actually used a script<\/I> to perform this task.<\/P>\n<P>And you guys thought the Scripting Guys talked the talk without walking the walk.<\/P>\n<P>Not <I>this<\/I> Scripting Guy, though (or at least not on this one occasion). Instead, he used a script very similar to this one to rename his picture files:<\/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:\\Photos&#8217;} Where &#8221; _\n        &amp; &#8220;ResultClass = CIM_DataFile&#8221;)<\/p>\n<p>For Each objFile In colFiles\n    strEnd = Right(objFile.Name, 8)\n    strNewName = objFile.Drive &amp; objFile.Path &amp; &#8220;272_DSC_&#8221; &amp; strEnd\n    errResult = objFile.Rename(strNewName)\nNext\n<\/PRE>\n<P>A couple things to note here. First, we\u2019re assuming that you want to rename <I>all<\/I> the files in a particular folder. If that\u2019s not the case, then you\u2019ll have to include additional code to filter out files that shouldn\u2019t be renamed. Second, we\u2019re assuming that each photo ends with four digits and a <B>.jpg<\/B> file extension. If that\u2019s not true, (for example, if you have files with names like _JKG1.jpg), well, then this particular script won\u2019t help you. You can still rename files that have names like that, but the code is a bit more complicated. But seeing as how all the digital cameras we\u2019re familiar with tack a four-digit number at the end of the file name, we\u2019ll assume that\u2019s true in your case as well.<\/P>\n<P>The script begins by connecting to the WMI service on the local computer, then uses an <B>Associators Of<\/B> query to return a collection of all the files in the folder C:\\Photos:<\/P><PRE class=\"codeSample\">Set colFiles = objWMIService.ExecQuery _\n    (&#8220;ASSOCIATORS OF {Win32_Directory.Name=&#8217;C:\\Photos&#8217;} Where &#8221; _\n        &amp; &#8220;ResultClass = CIM_DataFile&#8221;)\n<\/PRE>\n<P>Admittedly, an Associators Of query is weird-looking. But don\u2019t worry about that; instead, just replace C:\\Photos with the path to your picture files and leave everything else alone.<\/P>\n<P>We then use this block of code to walk through the collection of picture files:<\/P><PRE class=\"codeSample\">For Each objFile In colFiles\n    strEnd = Right(objFile.Name, 8)\n    strNewName = objFile.Drive &amp; objFile.Path &amp; &#8220;272_DSC_&#8221; &amp; strEnd\n    errResult = objFile.Rename(strNewName)\nNext\n<\/PRE>\n<P>Inside the loop we take a look at the <B>Name<\/B> of the first file; that\u2019s going to be something like C:\\Photos\\_JKG1234.jpg. (In WMI, the Name property of a file is what we would call the file path.) We then use the VBScript <B>Right<\/B> function to grab the rightmost eight characters in the Name; in this case, that will be 1234.jpg (C:\\Photos\\_JKG<B>1234.jpg<\/B>). Those are eight characters we want to preserve (we don\u2019t want to change the numbering or the file extension), so we stash them in a variable named strEnd.<\/P>\n<P>Next we construct a new name for our file; that\u2019s what we do here:<\/P><PRE class=\"codeSample\">strNewName = objFile.Drive &amp; objFile.Path &amp; &#8220;272_DSC_&#8221; &amp; strEnd\n<\/PRE>\n<P>When you rename a file using WMI you need to specify the complete file path; you can\u2019t just rename a file something like NewName.jpg. Because of that we need to concatenate the drive letter for the file (C:), the path (in WMI terms, the folder path without the drive letter, or \\Photos\\), the new file name prefix (272_DSC_), and the eight characters we preserved from the existing file name. In other words:<\/P><PRE class=\"codeSample\">C: + \\Photos\\ + 272_DSC_ + 1234.jpg\n<\/PRE>\n<P>Put them all together and they spell <B>C:\\Photos\\272_DSC_1234.jpg<\/B>. And that\u2019s good, because that\u2019s exactly what we want the new file path to be.<\/P>\n<P>From there we simply call the <B>Rename<\/B> method, passing the new file path as the sole method parameter. We then loop around and repeat the process for the remaining files in the collection.<\/P>\n<P>And you know what, NA? We agree with you: knowledge like this is <I>far<\/I> more valuable than a winning lottery ticket. Just keep telling yourself that: far more valuable. <\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I rename files like _JKG1234.jpg to 272_DSC_1234.jpg?&#8212; NA Hey, NA. You know, NA, we feel sorry for you. Why? Well, on the one hand you\u2019re unbelievably lucky. After all, the Scripting Guys never write scripts that they actually use, yet you somehow managed to hit upon an exception. Talk about [&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":[715,38,3,4,12,5,6],"class_list":["post-68453","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-associators-of","tag-files","tag-scripting-guy","tag-scripting-techniques","tag-storage","tag-vbscript","tag-wmi"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I rename files like _JKG1234.jpg to 272_DSC_1234.jpg?&#8212; NA Hey, NA. You know, NA, we feel sorry for you. Why? Well, on the one hand you\u2019re unbelievably lucky. After all, the Scripting Guys never write scripts that they actually use, yet you somehow managed to hit upon an exception. Talk about [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68453","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=68453"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68453\/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=68453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}