How Can I Save a File Based on the Day of the Week?

ScriptingGuy1

Hey, Scripting Guy! Question

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.

— TW

SpacerHey, Scripting Guy! AnswerScript Center

Hey, TW. You know, the problem with the Scripting Guy who writes this column is that he – OK, fine: one of the problems with the Scripting Guy who writes this column is that he’s 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’t 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 limit Internet use: none of their subjects could stop using the Internet altogether, even though they were being paid to do so.

Meanwhile, the Scripting Guy who writes this column just came back from a week off, a week spent partly at Mom and Dad’s 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’t use much technology of any kind during those nine days. But did anyone pay him for staying away from technical gizmos and gadgets? You already know the answer.

Editor’s Note: We’re betting the Scripting Guy used the refrigerator quite a bit. Given that Mom and Dad live a hundred miles or so away we’re betting he didn’t walk there, so some technology was involved in the trip. There’s probably some sort of filtration system on the swimming pool he’s been bragging about. It was a paid vacation. No, we’re not feeling too sorry for the Scripting Guy.

All of which means it’s important to be in the right place at the right time: it’s 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’s 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’s 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’s how:

strWeekDay = Weekday(Date)
strDay = WeekdayName(strWeekDay)

strPath = “C:\Logs\” & strDay & “\Results.txt”

Set objFSo = CreateObject(“Scripting.FileSystemObject”) Set objFile = objFSO.CreateTextFile(strPath)

objFile.WriteLine “This is a test.” objFile.Close

Of course it’s a simple little script. After all, the Scripting Guy who writes this column was only gone for a week; that’s not enough time to forget how to be lazy. That’s why this was the perfect question for him: he didn’t have to do much work, yet he was still able to provide a solution. And best of all, he got paid for it.

Note. Or at least he assumes he got paid for it. After all, Microsoft uses electronic deposit, and this Scripting Guy never bothers to verify that his check actually got deposited. Maybe the folks at Microsoft are smarter than he gave them credit for ….

As it turns out, the key lines in this script come right at the beginning:

strWeekDay = Weekday(Date)
strDay = WeekdayName(strWeekDay)

In line 1, we use the Weekday 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:

Value

Day of the Week

1

Sunday

2

Monday

3

Tuesday

4

Wednesday

5

Thursday

6

Friday

7

Saturday

Of course, that’s all fine and dandy, except for one thing: a 5 doesn’t really do you much good, does it? Because it’s a Thursday you want your data stored in the folder C:\Logs\Thursday, not C:\Logs\5. That’s a problem.

Or at least it would be, if it wasn’t for line 2, where we use the WeekdayName function to convert that 5 to the string value Thursday:

strDay = WeekdayName(strWeekDay)

After this line of code executes, the variable strDay will be equal to Thursday, which – fortunately for us – just happens to be the name of the folder where the file needs to be saved.

We should point out that, yes, we used two lines of code to retrieve the string value of Thursday. However, we could have performed this same task in a single line by “nesting” the functions:

strDay = WeekdayName(Weekday(Date))

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 Thursday. We’re 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.

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’s fine.

After all, it’s not like we’ve got too much other work to do.

After we have the string value corresponding to the day then we use this line of code to construct the file path:

strPath = “C:\Logs\” & strDay & “\Results.txt”

Nothing too special there: we simply combine the following items in order to create the path:

C:\Logs\

Thursday (the value of the variable strDay)

\Results.txt

The rest of the script then creates a sample text file and saves it to the proper folder:

Set objFSo = CreateObject(“Scripting.FileSystemObject”)
Set objFile = objFSO.CreateTextFile(strPath)

objFile.WriteLine “This is a test.” objFile.Close

That’s not very exciting, but we tossed it in just so you could see that the script really does work.

Well, OK: come to think of it is a little exciting, isn’t it?

Note: One thing to keep in mind is that this script doesn’t actually create the folder. (What do you expect right after vacation?) If the folder C:\Logs\Thursday doesn’t already exist when you run this script, you’ll get a “Path not found” error.

This should work just fine, but there is one caveat here: because you’re using the generic file name Results.txt it won’t be long (a week, in fact) before you start running into file name collisions. For example, on August 10th you save a file to C:\Logs\Results.txt. On August 17th you’re 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 17th.

If it’s not fine, well, no problem: here’s 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:

strWeekDay = Weekday(Date)
strDay = WeekdayName(strWeekDay)

strDate = Replace(Date,”/”,”-“)

strPath = “C:\Logs\” & strDay & “\Results_” & strDate & “.txt”

Set objFSo = CreateObject(“Scripting.FileSystemObject”) Set objFile = objFSO.CreateTextFile(strPath)

objFile.WriteLine “This is a test.” objFile.Close

The main thing we’ve done here is add a line of code that takes the current date, replaces the /’s with hyphens, and then stores the resulting value in a variable named strDate:

strDate = Replace(Date,”/”,”-“)

Why do we replace all the / characters? That’s easy: those characters aren’t allowed in file names. Hyphens are allowed, however, so we simply use the Replace 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.

Note. 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’s OK; this script will still work just fine. That’s because no error is generated if the Replace function can’t find any /’s to replace. Like the Scripting Guys, the Replace function is perfectly content to do nothing at all.

And, of course, we also need to add the variable strDate to the line of code where we construct the file path:

strPath = “C:\Logs\” & strDay & “\Results_” & strDate & “.txt”

Hope that answers your first question, TW. As to your unasked second question – whether or not the Scripting Guy who writes this column is happy to be back to work – let’s think about that for a moment. Last week he was out in the sun, taking an occasional dip in his parents’ swimming pool, playing Wiffle ball, staying up late and sleeping in. This week he’s getting up at 6:30 every morning and sitting in an office for 8 hours. You know, it’s hard to choose between those two options, isn’t it?

0 comments

Discussion is closed.

Feedback usabilla icon