{"id":65963,"date":"2006-11-28T07:53:00","date_gmt":"2006-11-28T07:53:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/11\/28\/how-can-i-rename-a-folder\/"},"modified":"2006-11-28T07:53:00","modified_gmt":"2006-11-28T07:53:00","slug":"how-can-i-rename-a-folder","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-rename-a-folder\/","title":{"rendered":"How Can I Rename a Folder?"},"content":{"rendered":"<p><img decoding=\"async\" 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\"><\/p>\n<p>Hey, Scripting Guy! How can I add a suffix to a folder name? For example, I\u2019d like to rename the folder C:\\January to C:\\January_2006.<\/p>\n<p>&#8212; NB<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><img decoding=\"async\" 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 decoding=\"async\" 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><\/p>\n<p>Hey, NB. You know, back in July the Scripting Guy who writes this column happened to wander through the living room while the Scripting Son was watching the world-famous Nathan\u2019s Hot Dog-Eating Contest on ESPN. (And no, don\u2019t worry: this isn\u2019t the start of some long and pointless rant against eating contests being broadcast on a sports channel. After all, if ESPN is going to show spelling bees, poker games, and soccer, well, why not hot dog-eating contests, too?) The Scripting Guy who writes this column watched for a few minutes and, truth be told, he was a bit repulsed by what he saw. Good heavens, he thought, how could people sit there and continually, and mindlessly, stuff themselves full of food like that? What is <i>wrong<\/i> with those people?!?<\/p>\n<p>Of course, that was this summer, long before the Thanksgiving holiday. Now, after four days of turkey, mashed potatoes and gravy, sweet potatoes, stuffing, rolls, stir-fried green beans and bacon, assorted Jellos and salads, pumpkin pie, apple-raspberry pie \u2013 well, let\u2019s just say that the Scripting Guy who writes this column now has a lot more empathy for people who mindlessly stuff themselves full of food.<\/p>\n<table id=\"EAD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td>\n<p><b>Note<\/b>. What\u2019s that? What was the Scripting Guy who writes this column thankful for this past holiday season? Mainly he was thankful that he has a job where he can just sit all day and not have to move very much.<\/p>\n<p>But don\u2019t worry: he\u2019ll be up and ready to go come time for Christmas dinner.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Fortunately, the Scripting Guy who writes this column doesn\u2019t have to move very much in order to answer your question, NB. You need a script that can add a suffix to a folder name? Here you go:<\/p>\n<pre class=\"codeSample\">strComputer = \".\"\nSet objWMIService = GetObject(\"winmgmts:\\\\\" &amp; strComputer &amp; \"\\root\\cimv2\")\nSet colFolders = objWMIService.ExecQuery _\n    (\"Select * From Win32_Directory Where Name = 'C:\\\\January'\")\nFor Each objFolder in colFolders\n    strNewName = objFolder.Name &amp; \"_2006\"\n    objFolder.Rename strNewName\nNext\n<\/pre>\n<p>Yes, this is a <i>very<\/i> simple script; what else would you expect for a column being written shortly after Thanksgiving? As you can see, we begin by connecting to the WMI service on the local computer, although \u2013 as with nearly all WMI scripts \u2013 we could just as easily run this script against a remote machine. (Meaning, in other words, that you can add a suffix to a folder found on a remote computer.) After making this connection we then use this line of code to retrieve a collection of all the folders on the computer that have a <b>Name<\/b> (which, in WMI-speak, is equivalent to the file path) equal to <i>C:\\January<\/i>:<\/p>\n<pre class=\"codeSample\">Set colFolders = objWMIService.ExecQuery _\n    (\"Select * From Win32_Directory Where Name = 'C:\\\\January'\")\n<\/pre>\n<p>And no, we didn\u2019t eat too much cranberry, walnut, and apple salad this weekend. Well, OK, actually we pretty much ate <i>all<\/i> of it, leaving only a spoonful or two for the rest of the family. However, that isn\u2019t why we typed the name like this: <b>C:\\\\January<\/b>. Instead, the two \\\u2019s are actually required; that\u2019s because the \\ is a reserved character in WMI, and any time you use a reserved character in a WMI query you need to \u201cescape\u201d that character by prefacing it with a \\. What if we wanted to rename the folder C:\\January\\Personnel\\Reviews? Then we\u2019d use a query like this one, taking care to escape each \\ in the path:<\/p>\n<pre class=\"codeSample\">Set colFolders = objWMIService.ExecQuery _\n    (\"Select * From Win32_Directory Where Name = 'C:\\\\January\\\\Personnel\\\\Reviews'\")\n<\/pre>\n<p>After executing the query we\u2019ll get back a collection consisting of all the folders on the computer that have the Name <i>C:\\January<\/i>. (And, because folder Names \u2013 or paths \u2013 must be unique, we know the collection will contain, at most, one item.) With the collection in hand we next set up a For Each loop to loop through each of the folders in that collection. Inside that loop, we construct a new name for the folder by taking the existing folder Name and adding the string value <i>_2006<\/i>, storing the new path (<i>C:\\January_2006<\/i>) in a variable named strNewName:<\/p>\n<pre class=\"codeSample\">strNewName = objFolder.Name &amp; \"_2006\"\n<\/pre>\n<p>Once we\u2019ve constructed the new name we can then use the <b>Rename<\/b> method to rename <i>C:\\January<\/i>, making sure that we pass the value strNewName as the method parameter:<\/p>\n<pre class=\"codeSample\">objFolder.Rename strNewName\n<\/pre>\n<p>And yes, in order to rename a folder you must pass the entire path for the new folder. Passing just the folder name (<i>January_2006<\/i>) won\u2019t do you any good.<\/p>\n<p>Because we probably <i>should<\/i> try to get a little exercise we decided to toss in a bonus script. Suppose you have a folder \u2013 C:\\Budget \u2013 that contains a bunch of subfolders (January, February, March, etc.). What if you wanted to tack a suffix onto each of those subfolders? No problem; this little script (which we won\u2019t discuss in any detail today) should do the trick:<\/p>\n<pre class=\"codeSample\">strComputer = \".\"\nSet objWMIService = GetObject(\"winmgmts:\\\\\" &amp; strComputer &amp; \"\\root\\cimv2\")\nstrFolderName = \"C:\\Budget\"\nSet colFolders = objWMIService.ExecQuery _\n    (\"Associators of {Win32_Directory.Name='\" &amp; strFolderName &amp; \"'} \" _\n        &amp; \"Where AssocClass = Win32_Subdirectory \" _\n            &amp; \"ResultRole = PartComponent\")\nFor Each objFolder in colFolders\n    strNewName = objFolder.Name &amp; \"_2006\"\n    objFolder.Rename strNewName\nNext\n<\/pre>\n<p>Wow; that was more tiring than we expected it to be.<\/p>\n<p>Incidentally, we know that many of you are dying to know who won this year\u2019s <a href=\"http:\/\/null\/technet\/scriptcenter\/resources\/qanda\/nov06\/hey1122.mspx\"><b>Terducken Bowl<\/b><\/a>. Unfortunately, we only managed to get halfway to 100: the game is currently tied at 49-49, and will have to be completed at Christmas. <\/p>\n<p>So why weren\u2019t we able to get in a complete game? Well, to begin with, two of the players (the Scripting Nephews) were late because they had to help clean house. (We were shocked, too: on the day of last year\u2019s Super Bowl did the Pittsburgh Steelers make Ben Roethlisberger and Jerome Bettis clean <i>their<\/i> houses before letting them leave for the game?) On top of that, the game was played in a hurricane, with winds gusting around 250,000 miles per hour. And no, that\u2019s not an exaggeration: at one point one of the Scripting Nephews threw a pass directly down the middle of the field. A huge gust of wind came up and not only blew the ball off course, it blew the ball completely off the field, over the fence and on into the neighbor\u2019s yard. <\/p>\n<p>But that\u2019s OK; if nothing else, this gives the Scripting Guy who writes this column something to look forward to come Christmas time. What\u2019s that? What about the food? Oh, sure, he\u2019ll probably eat <i>something<\/i>. But just to be polite; after all, he wouldn\u2019t want to hurt the Scripting Mom\u2019s feelings. Maybe just a little turkey, a little gravy, and a tiny sliver of pie. <\/p>\n<p>OK, maybe <i>two<\/i> tiny slivers of pie.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I add a suffix to a folder name? For example, I\u2019d like to rename the folder C:\\January to C:\\January_2006. &#8212; NB Hey, NB. You know, back in July the Scripting Guy who writes this column happened to wander through the living room while the Scripting Son was watching the world-famous [&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-65963","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! How can I add a suffix to a folder name? For example, I\u2019d like to rename the folder C:\\January to C:\\January_2006. &#8212; NB Hey, NB. You know, back in July the Scripting Guy who writes this column happened to wander through the living room while the Scripting Son was watching the world-famous [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65963","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=65963"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65963\/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=65963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}