Mert
Posts: 50
Joined: Mon Jun 15, 2009 4:00 pm

Scheduled task error

Hi I install a scheduled task to the target computer that runs at logon and this task needs sqlbrowser service to run properly but my task runs before the sqlbrowser service and I get an error during startup. Is there a way I can delay my task at startup or anything else I can do to solve this?
Bogdan
Posts: 2791
Joined: Tue Jul 07, 2009 7:34 am
Contact: Website

Re: Scheduled task error

Hi,

I've created a VBscript that waits for a specified amount of time and then runs an exe file, notepad.exe for example. Create a .vbs file containing this script, add the file in the Files and Folders page and then add it as a scheduled task.

You can modify the script to run whatever program you want and to wait as long as you wish. Here is the script:

Code: Select all

 'Here the program sleeps for 15000 miliseconds
WScript.Sleep(15000)

'This next part runs a path specified EXE

strProgramPath = "C:\Windows\notepad.exe"
set objShell = createobject("Wscript.Shell")
objShell.Run strProgramPath
Regards,
Bogdan
Bogdan Mitrache - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
Mert
Posts: 50
Joined: Mon Jun 15, 2009 4:00 pm

Re: Scheduled task error

Thanks this is what I needed but if the users install my program to a different path than C how can I get the path for my program?
Mert
Posts: 50
Joined: Mon Jun 15, 2009 4:00 pm

Re: Scheduled task error

I just tested the script but it is not working I get an error saying "System could not find the file specified"
Bogdan
Posts: 2791
Joined: Tue Jul 07, 2009 7:34 am
Contact: Website

Re: Scheduled task error

Hi,

Here is the solution. The installation path of your program is stored in the APPDIR property. You need the create a New File search in the Search page, thus locating the file or program required for your scheduled task.

This is the new code that will replace the one I've given to you earlier:

Code: Select all

' Here the program sleeps for 1000 milliseconds
WScript.Sleep(1000) 

'this next part runs a path specified EXE

strProgramPath = "|"
set objShell = createobject("Wscript.Shell")
objShell.Run strProgramPath
Now you shall create a new vbs file, different from the one above. Go to Custom Actions page, on Install -> New Installed Custom Action and select the new .vbs file created by you with the following code:

Code: Select all

Set oShell = CreateObject("WScript.Shell")

values = Session.Property("CustomActionData")

tokens = Split(values,"|")
   
' Get the installation directory
strAppDir = tokens(1)

' Set the current directory to APPDIR
oShell.CurrentDirectory = strAppDir

strOldText = "|"
strNewText = tokens(0)

ReplaceInFile "run.vbs", strOldText, strNewText

Function ReplaceInFile(strFileToUse, strOldText, strNewText)
   Const ForReading = 1
   Const ForWriting = 2
   
   Set objFSO = CreateObject("Scripting.FileSystemObject")
   Set objFile = objFSO.OpenTextFile(strFileToUse, ForReading)
   
   strText = ""
   strFile = ""
   
   strText = objFile.ReadAll
   objFile.Close
   strFile = Replace(strText, strOldText, strNewText)

   Set objFile = objFSO.OpenTextFile(strFileToUse, ForWriting)
   objFile.WriteLine strFile
   objFile.Close
End Function
In the Action Data field fill in this parameters, assuming your search is called "SEARCH":

Code: Select all

[SEARCH]|[APPDIR]
Regards,
Bogdan
Bogdan Mitrache - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
Mert
Posts: 50
Joined: Mon Jun 15, 2009 4:00 pm

Re: Scheduled task error

I tested the script but it is not working I guess it's because search is done before the installation I need to run the search after the exe is installed any way to do this?
Bogdan
Posts: 2791
Joined: Tue Jul 07, 2009 7:34 am
Contact: Website

Re: Scheduled task error

Hi,

I thought you are trying to schedule a program that is not installed by your package. For what you need you can set the Action Data field just as follows:

Code: Select all

[#name_of_your_file.exe]|[APPDIR]
a search is not required anymore. Here is the code for the two .vbs files with the proper changes.The script that will be added as a scheduled task:

Code: Select all

' Here the program sleep for 1000 miliseconds
WScript.Sleep(1000) 

'this next part runs a path specified EXE

strProgramPath = "path_of_the_file"
strCMD = CHR(34) & strProgramPath & CHR(34)
set objShell = createobject("Wscript.Shell")
objShell.Run strCMD
This is the code for the second .vbs file with the latest changes:

Code: Select all

Set oShell = CreateObject("WScript.Shell")
  
' Get the installation directory
values = Session.Property("CustomActionData")
tokens = Split(values,"|")
' Set the current directory to APPDIR
oShell.CurrentDirectory = tokens(1)

strOldText = "path_of_the_file"
strNewText = tokens(0)

'assuming the first .vbs file is called run.vbs this is how you call the function

ReplaceInFile "run.vbs", strOldText, strNewText

Function ReplaceInFile(strFileToUse, strOldText, strNewText)
   Const ForReading = 1
   Const ForWriting = 2
   
   Set objFSO = CreateObject("Scripting.FileSystemObject")
   Set objFile = objFSO.OpenTextFile(strFileToUse, ForReading)
   
   strText = ""
   strFile = ""
   
   strText = objFile.ReadAll
   objFile.Close
   strFile = Replace(strText, strOldText, strNewText)

   Set objFile = objFSO.OpenTextFile(strFileToUse, ForWriting)
   objFile.WriteLine strFile
   objFile.Close
End Function
Regards,
Bogdan
Bogdan Mitrache - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Common Problems”