Sign in to follow this  
Followers 0
Syed23

Error on converting VBS code to Autoit

3 posts in this topic

#1 ·  Posted (edited)

Hi All,

i found a below script from MS article which will be helpful to schedule the task. when i run the VBS file everything works fine but if i try to convert the vbs to Autoit i am getting below error message. can some one help me on this?

#include"Date.au3"
#include <WinAPI.au3>
;#include <AutoItErrorHandler_UDF.au3>
_WinAPI_GetLastError()
Opt("MustDeclareVars",1)
const $TriggerTypeTime = 1

const $ActionTypeExec = 0

Global $DateAdd,$XmlTime,$rootFolder,$DateAdd,$service,$currenttime1
$service = ObjCreate("Schedule.Service")
$service.Connect()

;;********************************************************
;; Get a folder to create a task definition in.
Dim $rootFolder
$rootFolder = $service.GetFolder("\")

;; The taskDefinition variable is the TaskDefinition object.
Dim $taskDefinition
;; The flags parameter is 0 because it is not supported.
$taskDefinition = $service.NewTask(0)

;********************************************************
; Define information about the task.

; Set the registration info for the task by
; creating the RegistrationInfo object.
Dim $regInfo
$regInfo = $taskDefinition.RegistrationInfo
$regInfo.Description = "Start notepad at a certain time"
$regInfo.Author = "Author Name"

;********************************************************
; Set the principal for the task
Dim $principal
$principal = $taskDefinition.Principal

; Set the logon type to interactive logon
$principal.LogonType = 3


; Set the task setting info for the Task Scheduler by
; creating a TaskSettings object.
Dim $settings
$settings = $taskDefinition.Settings
$settings.Enabled = True
$settings.StartWhenAvailable = True
$settings.Hidden = False

;********************************************************
; Create a time-based trigger.
Dim $triggers
$triggers = $taskDefinition.Triggers

Dim $trigger
$trigger = $triggers.Create($TriggerTypeTime)

; Trigger variables that define when the trigger is active.
Dim $startTime, $endTime

Dim $time
$currenttime1 = _Date_Time_GetSystemTime()
$time = $DateAdd("s", 30, $currenttime1)
$startTime = XmlTime($time)

$time = $DateAdd("n", 5,$currenttime1 )
$endTime = XmlTime($time)

MsgBox(0,"Start Time",$startTime)
MsgBox(0,"endTime :",$endTime)

$trigger.StartBoundary = $startTime
$trigger.EndBoundary = $endTime
$trigger.ExecutionTimeLimit = "PT5M"    ;Five minutes
$trigger.Id = "TimeTriggerId"
$trigger.Enabled = True

;***********************************************************
; Create the action for the task to execute.

; Add an action to the task to run notepad.exe.
Dim $Action
$Action = $taskDefinition.Actions.Create( $ActionTypeExec )
$Action.Path = "C:\Windows\System32\notepad.exe"

MsgBox(0,"Task Define", "Task definition created. About to submit the task...")

;***********************************************************
; Register (create) the task.

$rootFolder.RegisterTaskDefinition("Test TimeTrigger", $taskDefinition, 6,"" ,"" , 3)

MsgBox(0,"Submited", "Task submitted.")



;------------------------------------------------------------------
; Used to get the time for the trigger
; startBoundary and endBoundary.
; Return the time in the correct format:
; YYYY-MM-DDTHH:MM:SS.
;------------------------------------------------------------------
Func XmlTime($t)
    Global $cSecond, $cMinute, $CHour, $cDay, $cMonth, $cYear
    Global $tTime, $tDate,$t,$Second,$Minute,$Hour,$Day,$Month,$Year,$Right

    $cSecond = "0" & $Second($t)
    $cMinute = "0" & $Minute($t)
    $cHour = "0" & $Hour($t)
    $cDay = "0" & $Day($t)
    $cMonth = "0" & $Month($t)
    $cYear = $Year($t)

    $tTime = $Right($cHour, 2) & ":" & $Right($cMinute, 2) &":" & $Right($cSecond, 2)
    $tDate = $cYear & "-" & $Right($cMonth, 2) & "-" & $Right($cDay, 2)
    $XmlTime = $tDate & "T" & $tTime
EndFunc

Below is the error message:

C:\Users\Q03200\Desktop\TS.au3 (64) : ==> Missing right bracket ')' in expression.:
$time = $DateAdd("s", 30, $currenttime1)
$time = ^ ERROR

Below is the code which downloaded from MS Site:

'------------------------------------------------------------------
' This sample schedules a task to start notepad.exe 30 seconds
' from the time the task is registered.
'------------------------------------------------------------------

' A constant that specifies a time-based trigger.
const TriggerTypeTime = 1
' A constant that specifies an executable action.
const ActionTypeExec = 0   


'********************************************************
' Create the TaskService object.
Set service = CreateObject("Schedule.Service")
call service.Connect()

'********************************************************
' Get a folder to create a task definition in. 
Dim rootFolder
Set rootFolder = service.GetFolder("\")

' The taskDefinition variable is the TaskDefinition object.
Dim taskDefinition
' The flags parameter is 0 because it is not supported.
Set taskDefinition = service.NewTask(0) 

'********************************************************
' Define information about the task.

' Set the registration info for the task by 
' creating the RegistrationInfo object.
Dim regInfo
Set regInfo = taskDefinition.RegistrationInfo
regInfo.Description = "Start notepad at a certain time"
regInfo.Author = "Author Name"

'********************************************************
' Set the principal for the task
Dim principal
Set principal = taskDefinition.Principal

' Set the logon type to interactive logon
principal.LogonType = 3


' Set the task setting info for the Task Scheduler by
' creating a TaskSettings object.
Dim settings
Set settings = taskDefinition.Settings
settings.Enabled = True
settings.StartWhenAvailable = True
settings.Hidden = False

'********************************************************
' Create a time-based trigger.
Dim triggers
Set triggers = taskDefinition.Triggers

Dim trigger
Set trigger = triggers.Create(TriggerTypeTime)

' Trigger variables that define when the trigger is active.
Dim startTime, endTime

Dim time
time = DateAdd("s", 30, Now)  'start time = 30 seconds from now
startTime = XmlTime(time)

time = DateAdd("n", 5, Now) 'end time = 5 minutes from now
endTime = XmlTime(time)

WScript.Echo "startTime :" & startTime
WScript.Echo "endTime :" & endTime

trigger.StartBoundary = startTime
trigger.EndBoundary = endTime
trigger.ExecutionTimeLimit = "PT5M"    'Five minutes
trigger.Id = "TimeTriggerId"
trigger.Enabled = True

'***********************************************************
' Create the action for the task to execute.

' Add an action to the task to run notepad.exe.
Dim Action
Set Action = taskDefinition.Actions.Create( ActionTypeExec )
Action.Path = "C:\Windows\System32\notepad.exe"

WScript.Echo "Task definition created. About to submit the task..."

'***********************************************************
' Register (create) the task.

call rootFolder.RegisterTaskDefinition( _
    "Test TimeTrigger", taskDefinition, 6, , , 3)

WScript.Echo "Task submitted."



'------------------------------------------------------------------
' Used to get the time for the trigger 
' startBoundary and endBoundary.
' Return the time in the correct format: 
' YYYY-MM-DDTHH:MM:SS. 
'------------------------------------------------------------------
Function XmlTime(t)
    Dim cSecond, cMinute, CHour, cDay, cMonth, cYear
    Dim tTime, tDate

    cSecond = "0" & Second(t)
    cMinute = "0" & Minute(t)
    cHour = "0" & Hour(t)
    cDay = "0" & Day(t)
    cMonth = "0" & Month(t)
    cYear = Year(t)

    tTime = Right(cHour, 2) & ":" & Right(cMinute, 2) & _
        ":" & Right(cSecond, 2)
    tDate = cYear & "-" & Right(cMonth, 2) & "-" & Right(cDay, 2)
    XmlTime = tDate & "T" & tTime 
End Function
Edited by Syed23

Thank you,Regards,[font="Garamond"][size="4"]K.Syed Ibrahim.[/size][/font]

Share this post


Link to post
Share on other sites



You are mixing a Variable name with a UDFname.

That line should be:

$time = _DateAdd("s", 30, $currenttime1)

Jos :)


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

IIRC there is a UDF available to handle scheduled tasks. Search the Example Scripts forum and you will find.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

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
Sign in to follow this  
Followers 0