{"id":55303,"date":"2008-06-21T02:16:00","date_gmt":"2008-06-21T02:16:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/06\/21\/hey-scripting-guy-how-can-i-change-the-letter-case-of-my-file-names\/"},"modified":"2008-06-21T02:16:00","modified_gmt":"2008-06-21T02:16:00","slug":"hey-scripting-guy-how-can-i-change-the-letter-case-of-my-file-names","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-change-the-letter-case-of-my-file-names\/","title":{"rendered":"Hey, Scripting Guy! How Can I Change the Letter Case of My File Names?"},"content":{"rendered":"<p><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Question\" border=\"0\" title=\"Hey, Scripting Guy! Question\" class=\"nearGraphic\" \/><\/p>\n<p>Hey, Scripting Guy (and the $12 million-a-year Scripting Editor)! Is there a way to mass-rename all the files in a folder, changing the letter case as needed?<\/p>\n<p>&#8212; MV<\/p>\n<p><img decoding=\"async\" height=\"5\" width=\"5\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" alt=\"Spacer\" border=\"0\" \/><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Answer\" border=\"0\" title=\"Hey, Scripting Guy! Answer\" class=\"nearGraphic\" \/><a href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><img decoding=\"async\" height=\"288\" width=\"120\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" align=\"right\" alt=\"Script Center\" border=\"0\" title=\"Script Center\" class=\"farGraphic\" \/><\/a><\/p>\n<p>Hey, MV. To begin with, we should mention that we know that you actually asked two questions in your email. We&rsquo;re going to deal with the first question &ndash; mass-renaming of all the files in a folder, changing the letter case as needed &ndash; today, then address your other question sometime in the next week or so. Originally the plan was to answer both questions today: the Scripting Guy who writes this column would address the mass-renaming issue, and the Scripting Editor would tackle your other question (a question that has to do with changing the tags on MP3 files). Unfortunately, though, <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/jun08\/hey0618.mspx\"><strong>the $12 million-a-year Scripting Editor<\/strong><\/a> is not available today; she had to fly down to California to buy a pair of sunglasses.<\/p>\n<p>And yes, we know: that <i>does<\/i> sound a bit extravagant, flying all the way to California just to pick up a pair of sunglasses. To her credit, though, the Scripting Editor originally ran down to Walgreen&rsquo;s, hoping to pick up a pair of sunglasses off the rack. Unfortunately, Walgreen&rsquo;s didn&rsquo;t have what she was looking for; she needed sunglasses in 18K Yellow Gold, with 132 hand-set, full-cut diamonds, as well as a 2.0 carat Canary yellow diamond. And, of course, ivory buffalo horn temples and brown lenses with gold mirror. Sadly, she couldn&rsquo;t get ivory buffalo horn temples at Walgreen&rsquo;s; all their temples are made out of Alaskan walrus tusk. That left her no choice but to zip down to Studio City, CA and pick up a pair of sunglasses down there.<\/p>\n<table cellpadding=\"0\" cellspacing=\"0\" class=\"dataTable\" id=\"EHD\">\n<thead><\/thead>\n<tbody>\n<tr valign=\"top\" class=\"record\">\n<td>\n<p><strong>Note<\/strong>. OK, so maybe she didn&rsquo;t <i>have<\/i> to go down to California herself; she probably could have ordered them <a target=\"_blank\" href=\"http:\/\/www.luxuriatorcollection.com\/collection\/due23\"><strong>online<\/strong><\/a>. Which reminds us: if anyone out there is interested in being just like the Scripting Editor &ndash; and we have to assume that <i>everyone<\/i> is interested in being just like the Scripting Editor &ndash; these sunglasses are currently on sale for just $65,000. That means you can get both the sunglasses and a <a target=\"_blank\" href=\"http:\/\/www.cross.com\/catalog\/productdetail.aspx?cat_name=Century+Signet+Pen+and+Pencil+Sets&amp;id=AT0081-26\"><strong>$2,600 pen-and-pencil set<\/strong><\/a> for less than $70,000!<\/p>\n<p>Who said you can&rsquo;t find bargains anymore? You just have to be willing to <i>look<\/i> for them.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>At any rate, once the Scripting Editor returns from her skin tightening (<a target=\"_blank\" href=\"http:\/\/www.bluespa.com\/shop.php\"><strong>gift cards<\/strong><\/a> available!) she&rsquo;ll no doubt address MV&rsquo;s question about MP3 files. In the meantime, the Scripting Guy who writes this column doesn&rsquo;t need to have his skin tightened. (As far as he knows, anyway; after all, it hasn&rsquo;t fallen off or anything, at least not yet.) Therefore, he has time to go ahead and tackle MV&rsquo;s first question: is there a way to mass-rename all the files in a folder, changing the letter case as needed? <\/p>\n<p>The answer, of course, is yes, depending on what you mean by &ldquo;changing the letter case as needed.&rdquo; Let&rsquo;s start with a very simple example. Suppose you have files (in the folder C:\\Test) that look like this:<\/p>\n<pre class=\"codeSample\"><pre class=\"codeSample\">This is A file.Txt\nTHIS IS ANOTHER FILE.TXT\nthis is the third file.TXT\nTHIS IS THE LAST FILE.txt\n<\/pre>\n<p>Needless to say, this isn&rsquo;t the prettiest directory listing we&rsquo;ve ever seen. (Especially when compared to the <strong>Madera bathtub<\/strong>, hand-crafted from natural solid woods and priced to sell <br \/>at just $30,000 apiece. Pick up one for each member of the family; after all, you can never have too many hand-crafted wooden bathtubs.) Because of that, we might want to convert <br \/>all the letters in both the file name and the file extension to their lowercase equivalent, giving us a directory listing that looks like this:<\/p>\n<pre class=\"codeSample\"><pre class=\"codeSample\">this is a file.txt\nthis is another file.txt\nthis is the third file.txt\nthis is the last file.txt\n<\/pre>\n<p>So how are we going to do that? Why, by running the following script, of course:<\/p>\n<pre class=\"codeSample\"><pre class=\"codeSample\">strComputer = \".\"\n\nSet objWMIService = GetObject(\"winmgmts:\\\\\" &amp; strComputer &amp; \"\\root\\cimv2\")\n\nSet colFileList = objWMIService.ExecQuery _\n    (\"ASSOCIATORS OF {Win32_Directory.Name='C:\\Test'} Where \" _\n        &amp; \"ResultClass = CIM_DataFile\")\n\nFor Each objFile In colFileList\n    strName = LCase(objFile.FileName)\n    strExtension = LCase(objFile.Extension)\n\n    strNewName = objFile.Drive &amp; objFile.Path &amp; strName &amp; \".\" &amp; strExtension\n    objFile.Rename(strNewName)\nNext\n<\/pre>\n<p>Let&rsquo;s see what we have here. We kick things off by connecting to the WMI service on the local computer. And yes, we <i>can<\/i> run this script &ndash; and thus rename files &ndash; on a remote computer; <br \/>all we have to do is assign the name of that remote machine to the variable strComputer:<\/p>\n<pre class=\"codeSample\">strComputer = \"atl-fs-001\"<\/pre>\n<p>After connecting to the WMI service we then use this line of code to return a collection of all the files found in the folder C:\\Test:<\/p>\n<pre class=\"codeSample\"><pre class=\"codeSample\">Set colFileList = objWMIService.ExecQuery _\n    (\"ASSOCIATORS OF {Win32_Directory.Name='C:\\Test'} Where \" _\n        &amp; \"ResultClass = CIM_DataFile\")\n<\/pre>\n<p>And now it&rsquo;s time to have some fun. (As much fun as an $18,000 <a target=\"_blank\" href=\"http:\/\/www.outdoor-pool-tables.com\/\"><strong>stainless steel pool table<\/strong><\/a>? Well, no. But close. Very close.)<\/p>\n<p>To begin with, we set up a For Each loop that enables us to loop through all the files in the collection. Inside that loop the first thing we do is execute these two lines of code:<\/p>\n<pre class=\"codeSample\"><pre class=\"codeSample\">strName = LCase(objFile.FileName)\nstrExtension = LCase(objFile.Extension)\n<\/pre>\n<p>In the first line we&rsquo;re assigning a value to the variable strName. What value are we assigning? Well, we&rsquo;re taking the value of the <strong>FileName<\/strong> property and using the <strong>LCase<\/strong> function to <br \/>convert all the characters in that file name to their lowercase equivalent. In other words, suppose the name of our first file is <strong>This is A file<\/strong>. The LCase function will convert <br \/>that file name to the following:<\/p>\n<pre class=\"codeSample\">this is a file<\/pre>\n<p>In the second line of code we use the same approach to convert all the letters in the file extension to <i>their<\/i> lowercase equivalent.<\/p>\n<p>Believe it or not, at this point we&rsquo;re almost done. When you rename files using WMI you must supply the <strong>Rename<\/strong> method with a complete file path; this line of code helps us put together <br \/>a complete file path, substituting the variables strName and strExtension for the file name and the file extension:<\/p>\n<pre class=\"codeSample\">strNewName = objFile.Drive &amp; objFile.Path &amp; strName &amp; \".\" &amp; strExtension<\/pre>\n<p>All that&rsquo;s left now is to call the Rename method and rename the first file:<\/p>\n<pre class=\"codeSample\">objFile.Rename(strNewName)<\/pre>\n<p>And then it&rsquo;s back to the top of the list, where we repeat the process with the next file in the collection. When all is said and done, the folder C:\\Test should now look like this:<\/p>\n<pre class=\"codeSample\">this is a file.txt\nthis is another file.txt\nthis is the third file.txt\nthis is the last file.txt\nNot bad, eh? And, of course, you could easily convert all the letters in the file names to their <i>uppercase<\/i> equivalent. To do that, simply use the <strong>UCase<\/strong> function rather than the LCase <br \/>function.<\/pre>\n<p>Well that was easy, wasn&rsquo;t it? And fast, too: we&rsquo;ve still got time to do something else today. Enough time to spend a night at the <a target=\"_blank\" href=\"http:\/\/www.forbes.com\/2002\/03\/07\/0307feat_2.html\"><strong>Atlantis Bridge <\/strong><strong>Suite in the Atlantis <\/strong><strong>Hotel in the Bahamas<\/strong><\/a> <br \/>($25,000 per night)? Sadly, no. (Besides, the Scripting Editor says the place is overrated, although she does like the fact that the kitchen has a separate entrance, meaning that she never <br \/>actually has to see the cook or butler.) However, we <i>do<\/i> have time to tackle a more ambitious script, one that applies &ldquo;title casing&rdquo; to our file names. In other words, a script that can turn our <br \/>file names into these:<\/p>\n<pre class=\"codeSample\"><pre class=\"codeSample\">This Is A File.txt\nThis Is Another File.txt\nThis Is The Third File.txt\nThis Is The Last File.txt\n<\/pre>\n<p>How are we going to do <i>that<\/i>? Good question; maybe we should try running this script and see what happens:<\/p>\n<pre class=\"codeSample\">strComputer = \".\"\n\nSet objWMIService = GetObject(\"winmgmts:\\\\\" &amp; strComputer &amp; \"\\root\\cimv2\")\n\nSet colFileList = objWMIService.ExecQuery _\n    (\"ASSOCIATORS OF {Win32_Directory.Name='C:\\Test'} Where \" _\n        &amp; \"ResultClass = CIM_DataFile\")\n\nFor Each objFile In colFileList\n    strName = objFile.FileName\n    strExtension = LCase(objFile.Extension)\n\n    arrWords = Split(strName, \" \")\n\n    For i = 0 to UBound(arrWords)\n        intLength = Len(arrWords(i))\n        strFirstLetter = UCase(Left(arrWords(i), 1))\n        strOtherLetters = LCase(Right(arrWords(i), intLength - 1))\n        strName = strFirstLetter &amp; strOtherLetters\n        arrWords(i) = strName\n    Next\n\n    strNewName = Join(arrWords, \" \")\n    strNewName = objFile.Drive &amp; objFile.Path &amp; strNewName &amp; \".\" &amp; strExtension\n    objFile.Rename(strNewName)\nNext\nSo how does <i>this<\/i> script work? Well, in many ways it works the same as the first script we showed you: it connects to the WMI service, retrieves a collection of all the files <br \/>in the folder C:\\Test, and then assigns the file name and extension to a pair of variables:<\/pre>\n<pre class=\"codeSample\">strName = objFile.FileName\nstrExtension = LCase(objFile.Extension)\nAt this point, however, things start to diverge a little. To begin with, we use the <strong>Split<\/strong> function to convert our file name (This is A file) to an array, an array consisting of the <br \/>following items:<\/pre>\n<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\" class=\"listBullet\">&bull;<\/td>\n<td class=\"listItem\">\n<p>This <\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" class=\"listBullet\">&bull;<\/td>\n<td class=\"listItem\">\n<p>is <\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" class=\"listBullet\">&bull;<\/td>\n<td class=\"listItem\">\n<p>A <\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" class=\"listBullet\">&bull;<\/td>\n<td class=\"listItem\">\n<p>file<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>As you can see, by splitting on the blank space we&rsquo;ve created an array where each item represents a single word in the file name. Once we&rsquo;ve done that we can <br \/>execute this block of code:<\/p>\n<pre class=\"codeSample\"><pre class=\"codeSample\">For i = 0 to UBound(arrWords)\n    intLength = Len(arrWords(i))\n    strFirstLetter = UCase(Left(arrWords(i), 1))\n    strOtherLetters = LCase(Right(arrWords(i), intLength - 1))\n    strName = strFirstLetter &amp; strOtherLetters\n    arrWords(i) = strName\nNext\n<\/pre>\n<p>What we&rsquo;ve done here is set up a For Next loop that runs from 0 to the number of items in the array (the &ldquo;upper bound&rdquo; of the array, something we can determine <br \/>using the <strong>UBound<\/strong> function). Inside that loop we use the <strong>Len<\/strong> function to determine the number of characters in the first item (word) in the array. From there we can <br \/>use this line of code to grab the first letter in that value and convert it to an uppercase letter:<\/p>\n<pre class=\"codeSample\">strFirstLetter = UCase(Left(arrWords(i), 1))<\/pre>\n<p>And then we can use <i>this<\/i> line of code to grab all the remaining characters in the value (that is, everything <i>except<\/i> the first character) and convert them to lowercase <br \/>letters:<\/p>\n<pre class=\"codeSample\">strOtherLetters = LCase(Right(arrWords(i), intLength - 1))<\/pre>\n<p>We next combine our uppercase first letter with our lowercase remaining letters, then assign that combined value back to the array item:<\/p>\n<pre class=\"codeSample\">strName = strFirstLetter &amp; strOtherLetters\narrWords(i) = strName\nWhat does <i>that<\/i> mean? Well, suppose the first item in the array was <i>tHIs<\/i>. After running through this For Next loop, that first item will now be <i>This<\/i>. In other words, we&rsquo;ve capitalized <br \/>the first letter in the word, and made the remaining letters all lowercase.<\/pre>\n<p>After we repeat this process for all the items in the array we use the <strong>Join<\/strong> function to convert the array back to a string, separating each item (word) with a blank space. In other <br \/>words, if we started out with this file name:<\/p>\n<pre class=\"codeSample\">This is A file<\/pre>\n<p>We&rsquo;re going to end up with <i>this<\/i> file name:<\/p>\n<pre class=\"codeSample\">This Is A File<\/pre>\n<p>At that point we simply call the <strong>Rename<\/strong> method to rename the file, then go back to the top of our For Each loop and try again with the next file in the collection.<\/p>\n<p>Is that really going to apply title casing to all our file names? Give it a try and see for yourself.<\/p>\n<p>That&rsquo;s all we have time for today, MV; like we said, we&rsquo;ll address your other question after the Scripting Editor gets back. In the meantime, the Scripting Guy who writes this <br \/>column needs to get some new sunglasses himself. However, he <i>will<\/i> simply pick up a pair at Walgreen&rsquo;s; the Scripting Guy who writes this column doesn&rsquo;t need to jet down <br \/>to California just to buy sunglasses. The truth is, he doesn&rsquo;t need sunglasses that have temples made of ivory buffalo horn. No, sir: as long as the sunglasses keep the glare <br \/>out of his eyes he&rsquo;s happy.<\/p>\n<p>Well, provided they also come with 132 hand-set, full-cut diamonds. But if the temples are made out of Alaskan walrus tusk? Hey, no problem; he&rsquo;s not picky.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy (and the $12 million-a-year Scripting Editor)! Is there a way to mass-rename all the files in a folder, changing the letter case as needed? &#8212; MV Hey, MV. To begin with, we should mention that we know that you actually asked two questions in your email. We&rsquo;re going to deal with the [&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,3,12,5],"class_list":["post-55303","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-scripting-guy","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy (and the $12 million-a-year Scripting Editor)! Is there a way to mass-rename all the files in a folder, changing the letter case as needed? &#8212; MV Hey, MV. To begin with, we should mention that we know that you actually asked two questions in your email. We&rsquo;re going to deal with the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55303","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=55303"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55303\/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=55303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=55303"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=55303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}