How to schedule end-month tasks in Scheduled Tasks?

  • Thread starter Thread starter W A Fairuz W M Shariff
  • Start date Start date
W

W A Fairuz W M Shariff

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.
 
W 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.

The Task Scheduler cannot do what you ask, but this can be done
with some additional batch file programming. The Task Scheduler
is little more than a 'toy' job scheduler. 8-(
 
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.

The current version of the Task Scheduler cannot handle end of month
tasks. You would have to invoke a batch file every day, like so:

@echo off
call c:\tools\eom.bat
c:\winnt\system32\ntbackup.exe . . .

eom.bat
======
@echo off
for /F "tokens=2,3,4 delims=/ " %%a in ('echo %date%') do set day=%%a & set
month=%%b & set year=%%c

set /a leap=%year% %% 4
if %leap%==0 (set eom=29) else (set eom=28)

echo 01 03 05 07 08 10 12 | find "%month%" > nul && set eom=31
echo 04 06 09 11 | find "%month%" > nul && set eom=30

if not %eom%==%day% exit

eom.bat will terminate the scheduled task on all days except on
the last day in the month.

You may have to adjust the line after "@echo off" to suit your date
code format. Mine looks like this:

Fri 16/01/2004
 
Thanks Phil & Pegasus. I was trying to avoid programming
but it looks like I have to do it anyway... :(
Thanks Pegasus for the batch file...
 
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 -----------
 
I was wondering whether or not it is possible at all to make the PC power on
automatically just like it can automatically power off...
 
Your question appears to have nothing to do with the subject
of this thread. I recommend that you start a new thread.
 
Why so? I'd like the PC to turn on automatically at the end of month or at
any other time specified and perform the scheduled tasks... Quite within
topic...
 
Your question is basically "How to turn on a PC automatically
at a given time". This thread deals with the topic of creating
an end-of-month event. You are, of course, welcome to expand
this thread; whether you get any replies remains to be seen.
 
Does it help to know that document.write DateSerial(2000, 1, 1) - 0.5
shows me 1999-12-31 12:00:00 ?

In other words, the last day of a month is -24h to -0h of the first day
of the next month.
 
"W A Fairuz W M Shariff" <(e-mail address removed)> wrote in message
news:053101c3dbe9$a61ae830$(e-mail address removed)...
> 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.


The current version of the Task Scheduler cannot handle end of month
tasks. You would have to invoke a batch file every day, like so:

@echo off
call c:\tools\eom.bat
c:\winnt\system32\ntbackup.exe . . .

eom.bat
======
@echo off
for /F "tokens=2,3,4 delims=/ " %%a in ('echo %date%') do set day=%%a & set
month=%%b & set year=%%c

set /a leap=%year% %% 4
if %leap%==0 (set eom=29) else (set eom=28)

echo 01 03 05 07 08 10 12 | find "%month%" > nul && set eom=31
echo 04 06 09 11 | find "%month%" > nul && set eom=30

if not %eom%==%day% exit

eom.bat will terminate the scheduled task on all days except on
the last day in the month.

You may have to adjust the line after "@echo off" to suit your date
code format. Mine looks like this:

Fri 16/01/2004

I modified the eom.bat to also detect last working day of a month (i.e. the Friday before if EOM is on a weekend). I also adjusted the token assignment for US locale. This will NOT detect long weekends.

eom.bat
======
@echo off
for /F "tokens=1,2,3,4 delims=/ " %%a in ('echo %date%') do set dow=%%a & set month=%%b & set day=%%c & set year=%%d

set /a leap=%year% %% 4
if %leap% equ 0 (set eom=29) else (set eom=28)

echo 01 03 05 07 08 10 12 | find "%month%" > nul && set eom=31
echo 04 06 09 11 | find "%month%" > nul && set eom=30

if "%dow%"=="Fri " set /a day=day+2

if %day% lss %eom% exit
 
Back
Top