{"id":65683,"date":"2007-01-23T00:24:00","date_gmt":"2007-01-23T00:24:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/01\/23\/hey-scripting-guy-how-can-i-create-a-new-folder-using-a-name-based-on-the-current-date\/"},"modified":"2007-01-23T00:24:00","modified_gmt":"2007-01-23T00:24:00","slug":"hey-scripting-guy-how-can-i-create-a-new-folder-using-a-name-based-on-the-current-date","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-create-a-new-folder-using-a-name-based-on-the-current-date\/","title":{"rendered":"Hey, Scripting Guy! How Can I Create A New Folder Using a Name Based on the Current Date?"},"content":{"rendered":"<h2><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\" \/> <\/h2>\n<p>Hey, Scripting Guy! How can I create a new folder each night, using a folder name based on the current date?<\/p>\n<p>&#8212; DM<\/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, DM. In answer to your unasked question, no, the Scripting Guy who writes this column did not get selected to sit on a jury; as a result, he&rsquo;s back to work, and back to writing this column. And, no, we don&rsquo;t know why he wasn&rsquo;t selected, although we suspect that his answer to the question, &ldquo;How would you go about deciding this case?&rdquo; might have been a factor. In retrospect, it might not have been a good idea for him to say, &ldquo;Just shoot them all and let God sort it out.&rdquo;<\/p>\n<table cellpadding=\"0\" cellspacing=\"0\" class=\"dataTable\" id=\"E2C\">\n<thead><\/thead>\n<tbody>\n<tr valign=\"top\" class=\"record\">\n<td>\n<p class=\"lastInCell\"><b>Editor&rsquo;s Note:<\/b> Having been rejected by the courts for jury duty, the Scripting Guy who writes this column is now apparently working on being rejected by Microsoft for his job. And while that would make the Scripting Editor&rsquo;s life much simpler, she still does her best to keep him from saying things like &ldquo;just shoot them&rdquo; in his articles. Sometimes, however, there&rsquo;s only so much one Scripting Editor can do.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Hey, we&rsquo;re just kidding; he didn&rsquo;t <i>really<\/i> say that (although his actual response might not have been much better). In truth, he was released from jury duty thanks to the enormous hue and cry raised by devoted readers of this column, readers who were concerned that they would not get their daily dose of <i>Hey, Scripting Guy!<\/i> during the three weeks while the trial took place.<\/p>\n<p>Well, OK, there really wasn&rsquo;t much of a hue, let alone a cry, although we assume that&rsquo;s because none of you had the address of the King County Superior Court. To be <i>really<\/i> honest about it, the Scripting Guy who writes this column was released solely due to chance: when all was said and done, the juror number he was randomly assigned mathematically eliminated him from consideration.<\/p>\n<p>Which only seems fair: after all, as near as he can tell, it was also due solely to random chance that he got his job as a Scripting Guy in the first place. Now he and universe are even!<\/p>\n<p>At any rate, the Scripting Guy who writes this column is back. And what better way to celebrate his return than by taking a look at a script that creates a new folder each night, using a folder name based on the current date:<\/p>\n<pre class=\"codeSample\">strMonth = Month(Date)\n\nIf Len(strMonth) = 1 Then\n    strMonth = \"0\" &amp; strMonth\nEnd If\n\nstrDay = Day(Date)\n\nIf Len(strDay) = 1 Then\n    strDay = \"0\" &amp; strDay\nEnd If\n\nstrYear = Year(Date)\n\nstrFolderName = \"C:\\Scripts\\Tammy_\" &amp; strMonth &amp; \"-\" &amp; strDay &amp; \"-\" &amp; strYear\n\nSet objFSO = CreateObject(\"Scripting.FileSystemObject\")\nSet objFolder = objFSO.CreateFolder(strFolderName)     <\/pre>\n<p>As you can see, there&rsquo;s not a whole lot to this script, and what little code we <i>do<\/i> have is primarily involved in coming up with the correct folder name. DM is looking for folder names similar to this: C:\\Scripts\\Tammy_01-22-2007. And whatever DM wants, DM gets.<\/p>\n<table cellpadding=\"0\" cellspacing=\"0\" class=\"dataTable\" id=\"E2D\">\n<thead><\/thead>\n<tbody>\n<tr valign=\"top\" class=\"record\">\n<td>\n<p class=\"lastInCell\"><b>Note<\/b>. Um, no, sorry, DM; you can&rsquo;t have that. Or that. Or &hellip;. And yes, we know what we said. But that was just a figure of speech.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>First things first: let&rsquo;s figure out how to create a name based on the current date (a task that we know a lot of you are interested in doing). To begin with, we assign the current month (using the <b>Month<\/b> function) to a variable named strMonth. That&rsquo;s going to assign strMonth the numeric value for the month; if the month is January, then strMonth will be equal to 1.<\/p>\n<p>Which, for a lot of you, is a problem: after all, you&rsquo;ve allotted two spaces for the month, meaning that you&rsquo;d really like strMonth to be equal to <i>0<\/i>1. That&rsquo;s OK; that&rsquo;s what this block of code is for:<\/p>\n<pre class=\"codeSample\">If Len(strMonth) = 1 Then\n    strMonth = \"0\" &amp; strMonth\nEnd If<\/pre>\n<p>Here we&rsquo;re using the <b>Len<\/b> function to determine the number of characters in the variable strMonth. If the number of characters (length) is equal to 1, as it will be in the month of January, we simply use this line of code to add a leading 0 to the value:<\/p>\n<pre class=\"codeSample\">strMonth = \"0\" &amp; strMonth<\/pre>\n<p>What if the number of characters is <i>not<\/i> equal to 1 (meaning that it must be equal to 2)? In that case we leave well enough alone and simply move on.<\/p>\n<p>Speaking of which, we next go ahead and repeat this same process to determine the day of month, this time using the <b>Day<\/b> function to grab the value and a similar block of code to add a leading 0 to that value (if necessary):<\/p>\n<pre class=\"codeSample\">strDay = Day(Date)\n\nIf Len(strDay) = 1 Then\n    strDay = \"0\" &amp; strDay\nEnd If<\/pre>\n<p>Finally, we use the <b>Year<\/b> function to store the current year in the variable strYear:<\/p>\n<pre class=\"codeSample\">strYear = Year(Date)<\/pre>\n<p>In this case, we don&rsquo;t need to worry about leading zeroes; that&rsquo;s because we&rsquo;ve allotted four spaces for the year, and, by default, the Year function returns a value (such as 2007) four digits longs.<\/p>\n<p>Once we&rsquo;ve determined the values for the month, day, and year we then use this line of code to construct the complete path to the new folder:<\/p>\n<pre class=\"codeSample\">strFolderName = \"C:\\Scripts\\Tammy_\" &amp; strMonth &amp; \"-\" &amp; strDay &amp; \"-\" &amp; strYear<\/pre>\n<p>Here we&rsquo;re simply concatenating the following values (assuming the date really <i>is<\/i> January 22, 2007), and storing the resulting string in a variable named strFolderName:<\/p>\n<pre class=\"codeSample\">C:\\Scripts\\Tammy_\n+                   01\n+                    -\n+                   22  \n+                    -\n+                 2007<\/pre>\n<p>The net result is the desired folder path: C:\\Scripts\\Tammy_01-22-2007.<\/p>\n<p>From here all we have to do is create an instance of the <b>Scripting.FileSystemObject<\/b>, then use the <b>CreateFolder<\/b> method to create a new folder:<\/p>\n<pre class=\"codeSample\">Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\nSet objFolder = objFSO.CreateFolder(strFolderName)<\/pre>\n<p>The one drawback to this script is that it works on only the local computer. What if you want to create this same folder on a remote computer? For one suggestion, see <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/sept04\/hey0901.mspx\"><b>this previous <\/b><b><i>Hey, Scripting Guy!<\/i><\/b><b> column<\/b><\/a>.<\/p>\n<p>OK. Now that we have our script how do we make sure that it runs each and every night? Well, the best way to do that is to simply configure the script as a scheduled task. When you do so, use Cscript.exe as the executable program and assign the full path to your script file as a command-line argument for Cscript.exe.<\/p>\n<p>So was the Scripting Guy who writes this column disappointed that he didn&rsquo;t actually get to serve on a jury? Well, in some ways; his case actually sounded pretty interesting. On the other hand, the trial <i>was<\/i> expected to last at least three weeks, and it&rsquo;s hard to believe that anything that doesn&rsquo;t involve a ball of some kind could hold this Scripting Guy&rsquo;s attention for three hours, let alone three weeks. Besides, he needed to come back to work: the other Scripting Guys were simply <i>lost<\/i> without him! Poor, helpless little things &hellip;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I create a new folder each night, using a folder name based on the current date? &#8212; DM Hey, DM. In answer to your unasked question, no, the Scripting Guy who writes this column did not get selected to sit on a jury; as a result, he&rsquo;s back to work, [&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":[13,11,3,4,12,5],"class_list":["post-65683","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-dates-and-times","tag-folders","tag-scripting-guy","tag-scripting-techniques","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I create a new folder each night, using a folder name based on the current date? &#8212; DM Hey, DM. In answer to your unasked question, no, the Scripting Guy who writes this column did not get selected to sit on a jury; as a result, he&rsquo;s back to work, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65683","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=65683"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65683\/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=65683"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65683"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65683"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}