Hey, Scripting Guy! How can I create a new folder each night, using a folder name based on the current date?
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’s back to work, and back to writing this column. And, no, we don’t know why he wasn’t selected, although we suspect that his answer to the question, “How would you go about deciding this case?” might have been a factor. In retrospect, it might not have been a good idea for him to say, “Just shoot them all and let God sort it out.”
Editor’s Note: 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’s life much simpler, she still does her best to keep him from saying things like “just shoot them” in his articles. Sometimes, however, there’s only so much one Scripting Editor can do.
Hey, we’re just kidding; he didn’t really 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 Hey, Scripting Guy! during the three weeks while the trial took place.
Well, OK, there really wasn’t much of a hue, let alone a cry, although we assume that’s because none of you had the address of the King County Superior Court. To be really 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.
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!
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:
strMonth = Month(Date) If Len(strMonth) = 1 Then strMonth = "0" & strMonth End If strDay = Day(Date) If Len(strDay) = 1 Then strDay = "0" & strDay End If strYear = Year(Date) strFolderName = "C:\Scripts\Tammy_" & strMonth & "-" & strDay & "-" & strYear Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFolder = objFSO.CreateFolder(strFolderName)
As you can see, there’s not a whole lot to this script, and what little code we do 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.
Note. Um, no, sorry, DM; you can’t have that. Or that. Or …. And yes, we know what we said. But that was just a figure of speech.
First things first: let’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 Month function) to a variable named strMonth. That’s going to assign strMonth the numeric value for the month; if the month is January, then strMonth will be equal to 1.
Which, for a lot of you, is a problem: after all, you’ve allotted two spaces for the month, meaning that you’d really like strMonth to be equal to 01. That’s OK; that’s what this block of code is for:
If Len(strMonth) = 1 Then strMonth = "0" & strMonth End If
Here we’re using the Len 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:
strMonth = "0" & strMonth
What if the number of characters is not equal to 1 (meaning that it must be equal to 2)? In that case we leave well enough alone and simply move on.
Speaking of which, we next go ahead and repeat this same process to determine the day of month, this time using the Day function to grab the value and a similar block of code to add a leading 0 to that value (if necessary):
strDay = Day(Date) If Len(strDay) = 1 Then strDay = "0" & strDay End If
Finally, we use the Year function to store the current year in the variable strYear:
strYear = Year(Date)
In this case, we don’t need to worry about leading zeroes; that’s because we’ve allotted four spaces for the year, and, by default, the Year function returns a value (such as 2007) four digits longs.
Once we’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:
strFolderName = "C:\Scripts\Tammy_" & strMonth & "-" & strDay & "-" & strYear
Here we’re simply concatenating the following values (assuming the date really is January 22, 2007), and storing the resulting string in a variable named strFolderName:
C:\Scripts\Tammy_ + 01 + - + 22 + - + 2007
The net result is the desired folder path: C:\Scripts\Tammy_01-22-2007.
From here all we have to do is create an instance of the Scripting.FileSystemObject, then use the CreateFolder method to create a new folder:
Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFolder = objFSO.CreateFolder(strFolderName)
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 this previous Hey, Scripting Guy! column.
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.
So was the Scripting Guy who writes this column disappointed that he didn’t actually get to serve on a jury? Well, in some ways; his case actually sounded pretty interesting. On the other hand, the trial was expected to last at least three weeks, and it’s hard to believe that anything that doesn’t involve a ball of some kind could hold this Scripting Guy’s attention for three hours, let alone three weeks. Besides, he needed to come back to work: the other Scripting Guys were simply lost without him! Poor, helpless little things ….