Hello,
W A Fairuz W M Shariff said:
Hi all,
I find it very difficult to schedule end-month tasks in
Scheduled Tasks. Currently, I have to set several
schedules for my end-month task as below:
1) To run on every 31st of Jan, Mar, May, Jul, Aug, Oct &
Dec
2) To run on 30th of the other months except Feb
3) To run on 28th of Feb
However, for #3, I have to manually change the schedule to
run on the 29th of Feb in leap years, e.g. 2004.
Can Scheduled Tasks handle end-month automatically?
Thank you in advance.
Here is an approach that uses a VB DLL which wraps
the Task Scheduler interfaces.
Copy the 2 scripts below into the same folder.
You need the VB object shown in the objectURL
tag below.
LDOM.wsf creates 3 triggers, one for each month
type (30,31,feb). I've tested the object and this
script on Win2k sp3.
The following WSH script works in Latin1 locales. I
don't know about Malaysia...
------ begin LDOM.wsf -----------
<job id="LDOM">
<comment>
'
' Author: (e-mail address removed)
' script: LDOM.wsf
' Description: Launch a script on the last day of every month
' keywords: last task schagent launch
' Date: 08/23/02
' objectURL:
http://mysite.verizon.net/res1ur2j/tasksch.htm
'
</comment>
<reference object="Scheduler.SchAgent" />
<script language="VBScript">
Option Explicit
dim day30, day31, day2928
dim oSch, oNew, lpdays
' test the constant via reference
'WScript.echo tfRunOnlyIfLoggedOn
day30 = tmJanuary OR tmApril OR tmJune OR tmAugust _
OR tmSeptember OR tmNovember
day31 = tmMarch OR tmMay OR tmJuly OR tmOctober OR tmDecember
day2928 = tmFebruary
' for date formats
setLocale "en-us"
''''''''''''''''
' main object
'''''''''''''''''
set oSch = createobject("Scheduler.SchAgent")
' constructor, nothing there till we do this
oSch.refresh
'WScript.echo TypeName( oSch )
on error resume next
''''''''''''''''''''
' create new job, delete old version first if necessary
''''''''''''''''''''''''''''''''''''''''''
err.clear
dim MyJob
MyJob = "LDOM"
set oNew = NewJob(MyJob)
' stop on any error
on error goto 0
oNew.ApplicationName = "Cscript.exe"
oNew.CommandLine = "LDOM.vbs null" ' can be self aware
oNew.Comment = "Remind me to send alimony."
oNew.WorkingDirectory = ScriptFolder() ' func call--see below
oNew.Creator = "Joe Hacker" ' ala B. McKinney
oNew.Flags = tfDontStartIfOnBatteries Or tfKillIfGoingOnBatteries
' comment below unless running on NT type OS: WinXP and Win2k
oNew.SetAccountInformation "", vbNullString
'oNew.SetAccountInformation "user", "password"
' check for leap year in this current year
' since we create this JOB in January of every year!
' remember to send the author of this script a
' cyber six-pack for all
' mouse clicks this script saved me
if IsLeap( Year(date) ) then
lpdays = 29
else
lpdays = 28
end if
''''''''''''''''''''''''''''''
' add three triggers to oNew
'''''''''''''''''''''''''''''''
WScript.echo NewTrig( day30, 30)
WScript.echo NewTrig( day31, 31)
WScript.echo NewTrig( day2928, lpdays )
''''''''''''''''''''''''''''''''
' refresh all collections
oNew.Triggers.Update
oNew.Save
oSch.Refresh
Wscript.echo oNew.RunTimes( #01/16/2004#, #01/15/2005#)
' end main
function NewTrig( MonTyp, MonDays) ' as string
dim oTrig
Set oTrig = oNew.Triggers.Add
With oTrig
' strange but this has to be set to use an EndDay
.flags = tfHasEndDate
.TriggerType = ttMonthlyDate
.BeginDay = #01/16/2004#
.EndDay = #01/15/2005#
.StartTime = #12:30:00 AM#
' more than one month is OK
.Months = MonTyp
.Day = MonDays
' try three times, in a 15 minute period
.Interval = 5
.Duration = 15
.Update
NewTrig= .Text
End with
end function
Function IsLeap(YearIn ) 'As Boolean
IsLeap = IsDate("2/29/" & YearIn)
End Function
function NewJob( MyJob ) ' as JOB
' trick to check for existance of MyJob
on error resume next
err.clear
if IsObject (oSch.Job(MyJob)) then
if err.number <> 0 then
WScript.echo "OK, All clear to create job"
'do nothing
err.clear
else
oSch.Delete(MyJob)
WScript.sleep 200
WScript.echo "deleting existing job"
end if
end if
Set NewJob = oSch.CreateTask(MyJob) ' return Job Object
end function
function ScriptFolder() ' as string
with WScript
ScriptFolder = Mid(.ScriptFullName,1, _
instrrev(.ScriptFullName,"\"))
end with
end function
</script>
</job>
------- end LDOM.wsf --------
This is the test script scheduled by the above
------- begin LDOM.vbs --------
dim WSHShell, oFso ' global
Set WSHShell = WScript.CreateObject("WScript.Shell")
set oFso = createobject("Scripting.FileSystemObject")
oFso.opentextfile( "MyLDOM.log", 8, _
vbTrue).writeline( _
"remember to send another alimony " & Now())
---------- end LDOM.vbs -----------