Jump to content

Scheduler


FreeRider
 Share

Recommended Posts

Hi all,

I know that windows already contains a scheduler and I also have seen some example scripts about that... but it was not applicable to my needs.

Therefore I decided to create my own one.

This small script permits to run task on a given day in the week (let's say Monday) but also to choose which occurrence of the month you want to.

An additional option makes possible on top of the "basic" task described above to test a "target date"... I explain :

You need to run a program each Monday following the first Friday (this is what I called the "target date") of the month... you can do it with this script.

#Region Func _OkToRun
    #cs ----------------------------------------------------------------------------
     AutoIt Version     :   3.2.10.0
     Author             :   Christophe Savard
     Script Function    :   Checks if today's the first Monday following the first friday of the month"
     Parameters         :   $RundDay (Mandatory)    -   Weekday number (integer) when the script must run  (1 = Sunday to 7 = Saturday) 
                            $DayToTest (Optional)   -   Day number (interger) of the day to test
                                                        Possible values :
                                                        Null        ==> $RunDay value is considered as the target (same as schedule a basic weekly task)
                                                        An interger ==> Value must be higher than the $RunDay value (otherwise it's a basic weekly run)     
                            $Occurence (Optional)   -   Occurrence of the $DayToTest in the month (Default = 1)
                                                        Possible values :
                                                        Null        ==> default value is used (the first Occurrence = 1) if $DayToTest value is not empty
                                                        -1          ==> The last Occurrence of the $DayToTest value if existing
                                                                    ==> All Occurrences of $RunDay value if $DayToTest value is blank (it's a weekly run in fact)
                                                        -2          ==> The last occurence of the $DayToTest value
                                                        An integer  ==> The value will be used if it's in the range of date between day 1 and the $RunDay value.
                                                                        If the value is out of range sript stops and return an error message
                                                                        e.g : You try to test the 5th Friday of the month and the current month only contains 4 Fridays.
                            $Occurence (Optional)   -   Full path to a log file
                            
    Sample syntax       :   If you want the script to run the Monday following the first Friday of the month the syntax will be : $OK = _OkToRun (2,6,1) or _OkToRun (2,6)
                            If you want the script to run each Monday then the syntax will be : OK = _OkToRun (2)
                                    
     Return value       :   On success - 1  
                            On failure - 0 (and place a formated error message in a writelogfile)
     Requirement(s)     :   #include <Date.au3>
    #ce ----------------------------------------------------------------------------
    Func _OkToRun ($RunDay,$DayToTest,$Occurrence = 1, $LogFullPath="")
        If $LogFullPath Then _FileWriteLog($LogFullPath,"===== START PROCESS " & _StringRepeat('=',80))
        Local $Retval = "", $Y, $M, $D
        #region Control of parameters validity and value
            If (Not IsInt ($RunDay) And $RunDay <> "") Or (IsInt ($RunDay) And ($RunDay > 7 Or $RunDay <= 0)) Then $Retval = $Retval & "Parameter 1 cannot be " & '"' & $RunDay & '"' & ". It must be an Integer between 1 & 7." & @LF
            If $Runday= "" Then $Retval = $Retval & "Parameter 1 is mandatory (a running day must be provided)." & @LF
            If (Not IsInt ($DayToTest) And $DayToTest <> "") Or  (IsInt ($DayToTest) And ($DayToTest > 7 Or $DayToTest <= 0) ) then $Retval = $Retval & "Parameter 2 cannot be " & '"' & $DayToTest & '"' & ". Possible values are : an Integer between 1 & 7 / Null value" & @LF
            If $DayToTest = $RunDay and $DayToTest <> "" Then $Retval = $Retval & "Parameter 2 cannot be equal to parameter 1. The target day to test cannot be the running day !" & @LF
            If (Not IsInt ($Occurrence) And $Occurrence <> "") Or (IsInt ($Occurrence) And ($Occurrence = 0 Or $Occurrence > 4)) Then $Retval = $Retval & "Parameter 3 cannot be " & '"' & $Occurrence & '"' & ". Possible values are : a number between 1 and 4 / [-1] for all Occurrences (weekly run) / [-2] for the last Occurrence / Null value" & @LF
            If $Occurrence = "" And $DayToTest <> "" Then $Occurrence = 1; Set the default value for Occurence only if $DayToTest value is not empty...
            If $Occurrence = "" And $DayToTest = "" Then $Occurrence = -1; Otherwise set $Occurrence value to "all" (equivalent to a weekly run)
            If $Occurrence <> "" And $DayToTest = "" Then $DayToTest = $RunDay; Weekly run but with an Occurrence condition (ex : 1st monday of the month)
            
            If $Retval Then; Prepare retval for logfile update
                $SplitedRetval = StringSplit($Retval,@LF)
                For $i = 1 To $SplitedRetval[0]
                    If $LogFullPath Then _FileWriteLog($LogFullPath,$SplitedRetval[$i])
                    If $i = $SplitedRetval[0]-1 Then ExitLoop
                Next
                If FileExists($LogFullPath) Then  _FileWriteLog($LogFullPath,"===== PROCESS ABORTED " &_StringRepeat('=',80))
                Return 0; Exit function if $Retval is not empty
            EndIf
            If @WDAY <> $RunDay Then; Exit if today is not the Day number required by $Runday parameter
                If $LogFullPath Then _FileWriteLog($LogFullPath, "Requested Run day is " & _DateDayOfWeek($RunDay,0) & " ==> Today is " & _DateDayOfWeek(@WDAY,0) & " !")
                If $LogFullPath Then _FileWriteLog($LogFullPath,"===== PROCESS ABORTED " &_StringRepeat('=',80))
                Return 0
            ElseIf @WDAY = $RunDay Then; Today's is the day...
                If $Occurrence = -1 Then
                    If $LogFullPath Then _FileWriteLog($LogFullPath,'Process run is granted !')
                    If $LogFullPath Then _FileWriteLog($LogFullPath,"===== END PROCESS " &_StringRepeat('=',80))
                    Return 1; and $Occurrence = -1 this means it's a weekly run in fact
                EndIf
            EndIf
            
        #endregion Control of parameters validity and value
        
        #region Offset Calculation
        If $DayToTest <> $Runday Then
            If $DayToTest > $Runday Then
                $OffSet = $Runday + (7 - $DayToTest); Calculates the offeset between $Runday and $DayToTest
            Else
                $OffSet = $Runday - $DayToTest
            EndIf
        EndIf
        #endregion Offset calculation
        
        #region $Occurrence consistancy test
        $ActualMthDayNbr = @MDAY; Gets the actual day number of the month
        
        Local $WeekDays[1] = [0] , $DayNum
        
        For $i=1 to $ActualMthDayNbr; loop to identify all the $Daytotest values in the month until today and gets the related dates
            $DayNum=_DateToDayOfWeek(@YEAR, @MON, $i)
            If $DayNum = $DayToTest Then
                $WeekDays[0] += 1; adds 1 to the array counter
                ReDim $WeekDays[$WeekDays[0]+1]; Increase the boundary
                If StringLen($i) = 1 Then
                    $WeekDays[$WeekDays[0]]= @YEAR & @MON & "0" & $i; Load the array element with the formated date.
                Else
                    $WeekDays[$WeekDays[0]]= @YEAR & @MON & $i; Load the array element with the formated date.
                EndIf
            EndIf
        Next

        If $Occurrence > $WeekDays[0] Then
            If $LogFullPath Then _FileWriteLog($LogFullPath,"Requested Occurrence is " & $Occurrence & " (parameter 3) " & "and the total available occurrences available up to now is " & $WeekDays[0] & "... ==> It's to early." &_StringRepeat('=',80))
            If $LogFullPath Then _FileWriteLog($LogFullPath,"===== PROCESS ABORTED " &_StringRepeat('=',80))
            Return 0
        EndIf
        #endregion $Occurrence consistancy test
        
        If @Compiled=0 Then _ArrayDisplay($WeekDays)
        
        #region Date checks
        If $Occurrence = -2 Then
            $DateOfDaytoTest=$WeekDays[$WeekDays[0]]; Takes the last value in the array and therefore the target date just before the rundate.
        Else
            $DateOfDaytoTest=$WeekDays[$Occurrence]; Takes the value following $Occurrence parameter
        EndIf
        
        If @Compiled = 0 Then MsgBox(0,"$DateOfDaytoTest",$DateOfDaytoTest)
        
        $JulianRunday = _DateToDayValue (@YEAR , @MON , @MDAY); Translate the $RundDay date into a julian date value
        _DayValueToDate ($JulianRunday - $Offset, $Y, $M, $D); Substracts the $Offset value to the $JulianRundDay value 
        If $Y & $M & $D = $DateOfDaytoTest Then; compare the previous result with the result obtained just above
            If $LogFullPath Then _FileWriteLog($LogFullPath,'Process run is granted !')
            If $LogFullPath Then _FileWriteLog($LogFullPath,"===== END PROCESS " &_StringRepeat('=',80))
            Return 1; Dates are the same => Run can be done
        Else
            If $LogFullPath Then _FileWriteLog($LogFullPath,"The target date is " &  $DateOfDaytoTest & " (Occurrence N° " & $Occurrence & " of the month)" & " and today's is to late !")
            If $LogFullPath Then _FileWriteLog($LogFullPath,"===== PROCESS ABORTED " &_StringRepeat('=',80))
            Return 0
        EndIf
        #endregion Date checks
    EndFunc
#endregion Func _OkToRun

As I just made some "light" tests, it's possible you will find some bugs, do not hesitate to report bugs.

I hope this will help someone...

Enjoy,

FreeRider.

FreeRiderHonour & Fidelity

Link to comment
Share on other sites

  • 2 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...