{"id":66643,"date":"2006-08-21T15:50:00","date_gmt":"2006-08-21T15:50:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/08\/21\/how-can-i-save-a-file-based-on-the-day-of-the-week\/"},"modified":"2006-08-21T15:50:00","modified_gmt":"2006-08-21T15:50:00","slug":"how-can-i-save-a-file-based-on-the-day-of-the-week","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-save-a-file-based-on-the-day-of-the-week\/","title":{"rendered":"How Can I Save a File Based on the Day of the Week?"},"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! I need to save data to a text file each evening. How can I determine the day of the week and then save the file in the appropriate folder? For example, when the script runs on a Thursday then the file should be saved to C:\\Logs\\Thursday\\Results.txt.<BR><BR>&#8212; TW<\/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, TW. You know, the problem with the Scripting Guy who writes this column is that he \u2013 OK, fine: <I>one<\/I> of the problems with the Scripting Guy who writes this column is that he\u2019s never in the right place at the right time. For example, a year or two ago there was a research study in which people were paid not to use the Internet. Or at least that was the idea: the researchers weren\u2019t more than a day or two into the study before their subjects began to experience withdrawal symptoms. In the end, the researchers had to merely <I>limit<\/I> Internet use: none of their subjects could stop using the Internet altogether, even though they were being paid to do so.<\/P>\n<P>Meanwhile, the Scripting Guy who writes this column just came back from a week off, a week spent partly at Mom and Dad\u2019s and partly at home. Did he use the Internet at any time during this week? No. Did he use a computer at any time during this week? No. In fact, other than the lawn mower (which he only used grudgingly), he didn\u2019t use much technology of <I>any<\/I> kind during those nine days. But did anyone pay <I>him<\/I> for staying away from technical gizmos and gadgets? You already know the answer.<\/P>\n<TABLE id=\"EKD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Editor\u2019s Note:<\/B> We\u2019re betting the Scripting Guy used the refrigerator quite a bit. Given that Mom and Dad live a hundred miles or so away we\u2019re betting he didn\u2019t walk there, so some technology was involved in the trip. There\u2019s probably some sort of filtration system on the swimming pool he\u2019s been bragging about. It was a <I>paid<\/I> vacation. No, we\u2019re not feeling too sorry for the Scripting Guy.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>All of which means it\u2019s important to be in the right place at the right time: it\u2019s important to the Scripting Guy (who could have been paid for doing nothing had he been in the right place at the right time) and it\u2019s important for you: you need to save your files into the right place at the right time. The problem for the Scripting Guy is that no one will pay him for doing nothing. (Well, technically Microsoft pays him and, in return, he usually does nothing. But that\u2019s different.) Your problem, meanwhile, is this: how do you determine the day of the week and then save your files to the appropriate folder? Here\u2019s how:<\/P><PRE class=\"codeSample\">strWeekDay = Weekday(Date)\nstrDay = WeekdayName(strWeekDay)<\/p>\n<p>strPath = &#8220;C:\\Logs\\&#8221; &amp; strDay &amp; &#8220;\\Results.txt&#8221;<\/p>\n<p>Set objFSo = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFile = objFSO.CreateTextFile(strPath)<\/p>\n<p>objFile.WriteLine &#8220;This is a test.&#8221;\nobjFile.Close\n<\/PRE>\n<P>Of course it\u2019s a simple little script. After all, the Scripting Guy who writes this column was only gone for a week; that\u2019s not enough time to forget how to be lazy. That\u2019s why this was the perfect question for him: he didn\u2019t have to do much work, yet he was still able to provide a solution. And best of all, he got paid for it.<\/P>\n<TABLE id=\"E2D\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Or at least he <I>assumes<\/I> he got paid for it. After all, Microsoft uses electronic deposit, and this Scripting Guy never bothers to verify that his check actually <I>got<\/I> deposited. Maybe the folks at Microsoft are smarter than he gave them credit for \u2026.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>As it turns out, the key lines in this script come right at the beginning:<\/P><PRE class=\"codeSample\">strWeekDay = Weekday(Date)\nstrDay = WeekdayName(strWeekDay)\n<\/PRE>\n<P>In line 1, we use the <B>Weekday<\/B> function to determine the weekday integer value corresponding to the current date. For example, suppose the current date is Thursday, August 10. In that case, Weekday returns a 5, which happens to be the value for Thursday. Where can you find a complete set of integer values? Why, right here, of course:<\/P>\n<TABLE id=\"ETE\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Value<\/B><\/P><\/TD>\n<TD>\n<P class=\"lastInCell\"><B>Day of the Week<\/B><\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">1<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Sunday<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">2<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Monday<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">3<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Tuesday<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">4<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Wednesday<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">5<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Thursday<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">6<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Friday<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">7<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Saturday<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Of course, that\u2019s all fine and dandy, except for one thing: a 5 doesn\u2019t really do you much good, does it? Because it\u2019s a Thursday you want your data stored in the folder C:\\Logs\\Thursday, not C:\\Logs\\5. That\u2019s a problem.<\/P>\n<P>Or at least it would be, if it wasn\u2019t for line 2, where we use the <B>WeekdayName<\/B> function to convert that 5 to the string value <I>Thursday<\/I>:<\/P><PRE class=\"codeSample\">strDay = WeekdayName(strWeekDay)\n<\/PRE>\n<P>After this line of code executes, the variable strDay will be equal to <I>Thursday<\/I>, which \u2013 fortunately for us \u2013 just happens to be the name of the folder where the file needs to be saved.<\/P>\n<P>We should point out that, yes, we used two lines of code to retrieve the string value of <I>Thursday<\/I>. However, we could have performed this same task in a single line by \u201cnesting\u201d the functions: <\/P><PRE class=\"codeSample\">strDay = WeekdayName(Weekday(Date))\n<\/PRE>\n<P>In this construction we use the Weekday function to determine the integer value of the current date; we then immediately apply the WeekdayName function to the returned value in order to get the string <I>Thursday<\/I>. We\u2019re able to do this because any time you nest functions VBScript begins with the innermost set of parentheses and works its way out. In other words, the script first runs the Weekday function and grabs the integer value for the current date; only after that function is complete does the script run the WeekdayName function and determine the string value corresponding to the integer value we got back.<\/P>\n<P>Why did we use two lines when one line would do? We like to make things as clear as possible when trying to explain these scripts and how they work. If that means writing an extra line of code or two, well, that\u2019s fine.<\/P>\n<P>After all, it\u2019s not like we\u2019ve got too much <I>other<\/I> work to do.<\/P>\n<P>After we have the string value corresponding to the day then we use this line of code to construct the file path:<\/P><PRE class=\"codeSample\">strPath = &#8220;C:\\Logs\\&#8221; &amp; strDay &amp; &#8220;\\Results.txt&#8221;\n<\/PRE>\n<P>Nothing too special there: we simply combine the following items in order to create the path:<\/P>\n<TABLE border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>C:\\Logs\\<\/B><\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>Thursday<\/B> (the value of the variable strDay)<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>\\Results.txt<\/B><\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>The rest of the script then creates a sample text file and saves it to the proper folder:<\/P><PRE class=\"codeSample\">Set objFSo = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFile = objFSO.CreateTextFile(strPath)<\/p>\n<p>objFile.WriteLine &#8220;This is a test.&#8221;\nobjFile.Close\n<\/PRE>\n<P>That\u2019s not very exciting, but we tossed it in just so you could see that the script really <I>does<\/I> work.<\/P>\n<P>Well, OK: come to think of it <I>is<\/I> a little exciting, isn\u2019t it?<\/P>\n<TABLE id=\"EEAAC\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note:<\/B> One thing to keep in mind is that this script doesn\u2019t actually create the folder. (What do you expect right after vacation?) If the folder C:\\Logs\\Thursday doesn\u2019t already exist when you run this script, you\u2019ll get a \u201cPath not found\u201d error.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>This should work just fine, but there is one caveat here: because you\u2019re using the generic file name Results.txt it won\u2019t be long (a week, in fact) before you start running into file name collisions. For example, on August 10<SUP>th<\/SUP> you save a file to C:\\Logs\\Results.txt. On August 17<SUP>th<\/SUP> you\u2019re going to try to do the same thing, except the file C:\\Logs\\Results.txt will already exist. That might be fine: you might want to overwrite the old file, or you might have removed the old file sometime before the 17<SUP>th<\/SUP>. <\/P>\n<P>If it\u2019s <I>not<\/I> fine, well, no problem: here\u2019s a slightly-modified script that tacks the date on to the end of the file name. That gives us a name like Results_08-10-2006.txt:<\/P><PRE class=\"codeSample\">strWeekDay = Weekday(Date)\nstrDay = WeekdayName(strWeekDay)<\/p>\n<p>strDate = Replace(Date,&#8221;\/&#8221;,&#8221;-&#8220;)<\/p>\n<p>strPath = &#8220;C:\\Logs\\&#8221; &amp; strDay &amp; &#8220;\\Results_&#8221; &amp; strDate &amp; &#8220;.txt&#8221;<\/p>\n<p>Set objFSo = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFile = objFSO.CreateTextFile(strPath)<\/p>\n<p>objFile.WriteLine &#8220;This is a test.&#8221;\nobjFile.Close\n<\/PRE>\n<P>The main thing we\u2019ve done here is add a line of code that takes the current date, replaces the \/\u2019s with hyphens, and then stores the resulting value in a variable named strDate:<\/P><PRE class=\"codeSample\">strDate = Replace(Date,&#8221;\/&#8221;,&#8221;-&#8220;)\n<\/PRE>\n<P>Why do we replace all the \/ characters? That\u2019s easy: those characters aren\u2019t allowed in file names. Hyphens <I>are <\/I>allowed, however, so we simply use the <B>Replace<\/B> function to swap in a hyphen anytime we see a \/. We start out with a date like 8\/10\/2006, and end up with a string value like 8-10-2006.<\/P>\n<TABLE id=\"EKBAC\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Depending on your Regional and Language options you might not have to do this; for example, some localized versions of Windows already use a hyphen to separate date parts. But that\u2019s OK; this script will still work just fine. That\u2019s because no error is generated if the Replace function can\u2019t find any \/\u2019s to replace. Like the Scripting Guys, the Replace function is perfectly content to do nothing at all.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>And, of course, we also need to add the variable strDate to the line of code where we construct the file path:<\/P><PRE class=\"codeSample\">strPath = &#8220;C:\\Logs\\&#8221; &amp; strDay &amp; &#8220;\\Results_&#8221; &amp; strDate &amp; &#8220;.txt&#8221;\n<\/PRE>\n<P>Hope that answers your first question, TW. As to your unasked second question \u2013 whether or not the Scripting Guy who writes this column is happy to be back to work \u2013 let\u2019s think about that for a moment. Last week he was out in the sun, taking an occasional dip in his parents\u2019 swimming pool, playing Wiffle ball, staying up late and sleeping in. This week he\u2019s getting up at 6:30 every morning and sitting in an office for 8 hours. You know, it\u2019s hard to choose between those two options, isn\u2019t it?<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I need to save data to a text file each evening. How can I determine the day of the week and then save the file in the appropriate folder? For example, when the script runs on a Thursday then the file should be saved to C:\\Logs\\Thursday\\Results.txt.&#8212; TW Hey, TW. You know, 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":[13,38,3,4,12,5],"class_list":["post-66643","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-dates-and-times","tag-files","tag-scripting-guy","tag-scripting-techniques","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I need to save data to a text file each evening. How can I determine the day of the week and then save the file in the appropriate folder? For example, when the script runs on a Thursday then the file should be saved to C:\\Logs\\Thursday\\Results.txt.&#8212; TW Hey, TW. You know, the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66643","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=66643"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66643\/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=66643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}