I'm using a single WSH script controlled by Microsoft Task Scheduler that calls a series of daily run batch files sequentially. However, some of these batch files only need to run on the first working day of each month. I don't want to create additional Task Scheduled events for these monthly tasks as I want to control and maintain everything from this single WSH script.
I'm using some functions within the script to work out if it's the the first working day of the month. If this is true, than those monthly batch files can be executed.
Below is some WSH code I've been working on and its been adapted from some Visual Basic code sourced at this site. http://www.tek-tips.com/viewthread.cfm?qid=147546&page=1941
Using the same concepts shown at that site, I have converted to it to WSH as best as could with some slight modifications and now have two basic functions called IsWorkingDay and IsFirstWorkingDay. The code has a bug and I just can't seem to get it right. It's works okay for April 2011 but not for May 2011.
I'd appeaciate any help in fixing this or making any conceptual improvements.
' ******************************************************************
' Determine if the current day is the first working day of the month.
' Working day is defined as any day from Monday to Friday.
iDayOfWeek = Weekday(Now)
wscript.echo "Day of Week as Numerical constant: " & iDayOfWeek
' Test if current day in the week is a working day
if (IsWorkingDay = True) then
Wscript.Echo "True - Today is a Working Day."
else
Wscript.Echo "False - Today is not a working day, it's the weekend."
end if
' ********************************************************************
' Function: IsWorkingDay
' Task: Determine if day of week is a working day (e.g. Mon to Fri)
' Input:
' Output: True/False
Function IsWorkingDay
iDow = Weekday(Now)
if (iDow >= 2) and (iDow <= 6) then
IsWorkingDay = True
else
IsWorkingDay = False
end if
End Function
' Today is the first working day this month if
' the previous working day was not in the month.
if (IsFirstWorkingDay = True) then
Wscript.Echo "True - It's the first working day of the month"
else
Wscript.Echo "False - It's not the first working day of the month"
end if
' ********************************************************************
' Function: IsFirstWorkingDay
' Task: Determine if it's the first working day of the month
' Input:
' Output: True/False
Function IsFirstWorkingDay
IsFirstWorkingDay = False
If (IsWorkingDay = True) Then
IsFirstWorkingDay = True ' Assume it is the first working day of the month
' but test the assumption.
Do
i = i + 1
If Month(Now - i) <> Month(now) Then
Exit do ' Eureka
End If
If IsWorkingDay Then ' was right to begin with
IsFirstWorkingDay = False
Exit do
End If
Loop
End If
End Function
Regards
Tony