{"id":64533,"date":"2007-07-03T00:30:00","date_gmt":"2007-07-03T00:30:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/07\/03\/how-can-i-create-a-folder-with-a-name-based-on-the-monday-and-friday-of-the-coming-week\/"},"modified":"2007-07-03T00:30:00","modified_gmt":"2007-07-03T00:30:00","slug":"how-can-i-create-a-folder-with-a-name-based-on-the-monday-and-friday-of-the-coming-week","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-create-a-folder-with-a-name-based-on-the-monday-and-friday-of-the-coming-week\/","title":{"rendered":"How Can I Create a Folder With a Name Based on the Monday and Friday of the Coming Week?"},"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! Every Sunday I need to create a folder with a name similar to this: <B>C:\\Test\\7-2-2007 to 7-6-2007<\/B>, with the dates representing the Monday and the Friday of the upcoming week. How can I write a script that will create this folder for me?<BR><BR>&#8212; R <\/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, R. You, from time-to-time people write to the Scripting Guys and say, \u201cYou guys are so crazy; where does all that craziness come from?\u201d Well, here\u2019s one place. As it turns out, later this summer the Scripting Guy who writes this column will be accompanying selected family members on a trip to Italy. <\/P>\n<TABLE class=\"dataTable\" id=\"E5C\" 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>. Were these family members selected because they were somehow better than the other family members? No. They were selected simply because they weren\u2019t a bunch of cheapskates, like the other family members.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>At any rate, this past weekend the tourists-to-be were sitting around discussing the forthcoming trip. \u201cAt least this time I won\u2019t be worried about the food,\u201d said the Scripting Mom, with memories of Parisian croissants filled with snails still fresh in her mind. \u201cI\u2019ll just have spaghetti or some other kind of pasta at every meal.\u201d<\/P>\n<P>\u201cI don\u2019t know, Mom,\u201d said the Scripting Sister. \u201cYou have to be careful in Italy, because spaghetti doesn\u2019t necessarily mean spaghetti and meatballs. What if you order spaghetti and it comes with clams or something?\u201d<\/P>\n<P>\u201cThat won\u2019t be a problem,\u201d said the Scripting Mom. \u201cI\u2019ll just make sure I order spaghetti with marijuana sauce each time.\u201d<\/P>\n<P>Now, to the Scripting Mom\u2019s credit she immediately corrected herself and said, \u201cMarinara sauce. I meant marinara sauce.\u201d Of course, by then the damage had already been done. \u201cI don\u2019t think you can get spaghetti with marijuana sauce in Rome,\u201d noted one of the Scripting Brothers. \u201cI think you\u2019ll have to go to Amsterdam to get that.\u201d<\/P>\n<TABLE class=\"dataTable\" id=\"END\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P><B>Official disclaimer<\/B>. We should note that neither Microsoft nor the Scripting Guys endorse the use of illicit drugs such as marijuana.<\/P>\n<P>And no, not even on spaghetti.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Anyway, if you\u2019re wondering why the Scripting Guys are so crazy, well, at least some of that appears to be genetic. Fortunately, though, the Scripting Guys don\u2019t spend <I>all<\/I> their time dining on spaghetti with marijuana sauce. Every now and then they pause for a moment and write scripts, scripts that do things like create a folder whose name includes the dates of the Monday and Friday of the coming week:<\/P><PRE class=\"codeSample\">intDay = Weekday(Date)<\/p>\n<p>If intDay = 1 Then\n    intAdder = 1\nElse\n    intAdder = 9 &#8211; intDay\nEnd If<\/p>\n<p>dtmMonday = Date + intAdder\ndtmFriday = dtmMonday + 4<\/p>\n<p>strFolderName = &#8220;C:\\Test\\&#8221; &amp; dtmMonday &amp; &#8221; to &#8221; &amp; dtmFriday\nstrFolderName = Replace(strFolderName, &#8220;\/&#8221;, &#8220;-&#8220;)<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFolder = objFSO.CreateFolder(strFolderName)\n<\/PRE>\n<P>Whoa, man: the colors, look at the colors \u2026.<\/P>\n<P>Sorry; the Scripting Mom packed a lunch for us this morning. For some reason, today\u2019s lunch seems to pack more of a wallop than the usual bologna sandwich and Twinkie. <\/P>\n<P>As for the script, as best we can recall, that starts out by using the <B>Weekday<\/B> function to assign a value representing the current day of the week to a variable name intDay. In case you\u2019re wondering, the Weekday function returns one of the following values, depending on the day of the week:<\/P>\n<TABLE class=\"dataTable\" id=\"EGE\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">Sunday<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">1<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">Monday<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">2<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">Tuesday<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">3<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">Wednesday<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">4<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">Thursday<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">5<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">Friday<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">6<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">Saturday<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">7<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Why do we do care about the current day of the week? Well, we thought it would be useful to create a script that can be run at any time; as created, you can run this script on any day of the week, not just on Sunday. That seemed better, and a little more generic, than hard-coding in values that would cause unexpected results of the script ran on any day but Sunday. Of course, to be <I>truly<\/I> generic we could modify the script so that it asked the user to input a date, then created a folder for the week immediately following which date was entered. But, then again, the Scripting Guys never want to be <I>truly<\/I> generic; we just want to be sort of generic. <\/P>\n<P>However, for those of you who don\u2019t mind being truly generic, just replace the first line of the script with the following block of code:<\/P><PRE class=\"codeSample\">dtmDate = InputBox(&#8220;Please enter a date: &#8220;, &#8220;Enter Date&#8221;)<\/p>\n<p>dtmDate = CDate(dtmDate)\nintDay = Weekday(dtmDate)\n<\/PRE>\n<P>This block of code asks the user to enter a date, uses the <B>CDate<\/B> function to convert that value to a date-time value, then uses the Weekday function to determine the day of the week corresponding to the entered date. Pretty slick , huh?<\/P>\n<P>Now, back to the question at hand: why <I>do<\/I> we care about knowing the current day of the week? That turns out to be an easy question to answer: our folder name has to start with the Monday of the following week, and we can\u2019t determine the date of the next Monday without knowing whether today happens to be a Tuesday, a Wednesday, or a whatever. Let\u2019s say that today is a Saturday; in that case, we\u2019re going to have to add 2 days to the current date to get the date of the next Monday. What if today <I>is<\/I> a Monday? In that case, we\u2019ll have to add 7 days to the current date to get the date of the next Monday. That\u2019s what this bit of code is for:<\/P><PRE class=\"codeSample\">If intDay = 1 Then\n    intAdder = 1\nElse\n    intAdder = 9 &#8211; intDay\nEnd If\n<\/PRE>\n<P>This code might not make a lot of sense at first, but it works. (Trust us.) What we\u2019re doing here is checking to see if intDay is equal to 1, meaning that we\u2019re running the script on a Sunday; if it is, then we assign 1 to a new variable named intAdder. As the name at least <I>kind<\/I> of implies, we\u2019re going to use intAdder to add the requisite number of days to the current date, eventually ending up with the date of the next Monday. If the script is run on a Sunday, then we have just 1 day until the next Monday. Consequently, we assign the value 1 to intAdder.<\/P>\n<P>If we run the script on any day <I>but<\/I> a Sunday, however, we execute this line of code instead:<\/P><PRE class=\"codeSample\">intAdder = 9 &#8211; intDay\n<\/PRE>\n<P>Had we wanted to, we could have set up a bunch of If Then statements, one for each day of the week. (For example, if intDay equals 2, then add 7 days to the current date.) We wanted to find a way to bypass writing all those If Then statements, however, so we used this simple little algorithm instead: subtract the integer value of the current day from 9 and, lo and behold, we\u2019ll get the back the number of days we need to add. For example, suppose today is a Tuesday, which has an integer value of 3. Subtract 3 from 9 and we get 6. Amazingly enough, 6 also happens to be number of days we need to add to a Tuesday in order to get the value of the very next Monday.<\/P>\n<P>And no, you haven\u2019t been slipped some of the Scripting Mom\u2019s spaghetti. Play with this a little bit and you\u2019ll see how it works.<\/P>\n<P>Now we\u2019re ready to start calculating dates and creating folder names. To begin with, we determine the date of the next Monday by taking the current date and adding the value of intAdder:<\/P><PRE class=\"codeSample\">dtmMonday = Date + intAdder\n<\/PRE>\n<P>If today happens to be Monday, July 2, 2007 then intAdder will be 7; adding 7 to July 2, 2007 makes the variable dtmMonday equal to this: July 9, 2007.<\/P>\n<P>If you thought that calculating the value of the next Monday was easy, wait until you see how easy it is to calculate the value of the next <I>Friday<\/I>; all you have to do is add 4 days to the date of the next Monday:<\/P><PRE class=\"codeSample\">dtmFriday = dtmMonday + 4\n<\/PRE>\n<P>As soon as we have our two dates we can then start putting together our folder name. Step 1 in that process is to execute this line of code:<\/P><PRE class=\"codeSample\">strFolderName = &#8220;C:\\Test\\&#8221; &amp; dtmMonday &amp; &#8221; to &#8221; &amp; dtmFriday\n<\/PRE>\n<P>One small word of caution here: depending on your language and regional settings you might have to modify the code used to create the folder name. On the Scripting Guys\u2019 test computer, dates are displayed in this format: <I>Month\/Day\/Year.<\/I> Thus the preceding line of code concatenates the following values:<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>C:\\Test<\/B>, the root folder where the new folder will be created.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>7\/7\/2007<\/B>, the date of the next Monday.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>to<\/B> , the word \u201c to \u201d (with a blank space before and after the word). As if you couldn\u2019t see that for yourself.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>7\/6\/2006<\/B>, the date of the next Friday.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>That\u2019s going to result in the following folder path:<\/P><PRE class=\"codeSample\">C:\\Test\\7\/7\/2007 to 7\/6\/2007\n<\/PRE>\n<P>That\u2019s <I>close<\/I> to what we want, except for all those \/ characters. Those aren\u2019t allowed in folder names; therefore, we use our next line of code the replace each \/ with a dash (-):<\/P><PRE class=\"codeSample\">strFolderName = Replace(strFolderName, &#8220;\/&#8221;, &#8220;-&#8220;)\n<\/PRE>\n<P>That gives us a folder path that looks like this:<\/P><PRE class=\"codeSample\">C:\\Test\\7-7-2007 to 7-6-2007\n<\/PRE>\n<P>That also means we\u2019re now ready to create an instance of the <B>Scripting.FileSystemObject<\/B> object and then use the <B>CreateFolder<\/B> method to create our new folder:<\/P><PRE class=\"codeSample\">Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFolder = objFSO.CreateFolder(strFolderName)\n<\/PRE>\n<P>At that point, we\u2019re done. Which can only mean one thing: it must be time for more spaghetti with \u2026marinara\u2026 sauce. If you need us, we\u2019ll be in the lunchroom.<\/P>\n<P>For the rest of the day.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Every Sunday I need to create a folder with a name similar to this: C:\\Test\\7-2-2007 to 7-6-2007, with the dates representing the Monday and the Friday of the upcoming week. How can I write a script that will create this folder for me?&#8212; R Hey, R. You, from time-to-time people write to [&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":[11,3,12,5],"class_list":["post-64533","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-folders","tag-scripting-guy","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! Every Sunday I need to create a folder with a name similar to this: C:\\Test\\7-2-2007 to 7-6-2007, with the dates representing the Monday and the Friday of the upcoming week. How can I write a script that will create this folder for me?&#8212; R Hey, R. You, from time-to-time people write to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64533","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=64533"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64533\/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=64533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64533"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}