Jump to content

Error on converting VBS code to Autoit


Syed23
 Share

Recommended Posts

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]

Link to comment
Share on other sites

  • Developers

You are mixing a Variable name with a UDFname.

That line should be:

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

Jos :)

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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
 Share

  • Recently Browsing   0 members

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