Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a scheduled task progrmatically.

Status
Not open for further replies.

2020Tech

Programmer
May 13, 2005
2
CA
Hello All.

Is it possible to create a scheduled task progmatically using foxpro 8.0SP1 on a windows XP machine.

I am unable to find any example code or documentation on this subject.

Thx for the help.
 
Run the following code in VFP and then go look at your Scheduled tasks... you should see notepad.exe scheduled to run every weekday at a certain time.
Code:
JobID = 0
lcComputer = "."
loWMIService = GetObject("winmgmts:" ;
+ "{impersonationLevel=impersonate}!\\" + lcComputer + "\root\cimv2")
loNewJob = loWMIService.Get("Win32_ScheduledJob")
errJobCreated = loNewJob.Create("Notepad.exe", "********123000.000000-420", .t. , BITOR(1, 2, 4, 8, 16), , , JobID)
?errJobCreated

boyd.gif

 
Thx for the reply craig but I found that the above code does not work for scheduled taks that have been created using the scheduled task wizard.

I came up with the following

Code:
* SCHEDULED TASKS
LOCAL lcFile, lcTasks, lcLine as String 
LOCAL lnFileHndl, lnSize as Integer 

lcFile = "C:\Test.txt"
ii = 0

* Get a Listing of the scheduled tasks currently on the computer
* SCHTASKS /? From command prompt for Help
RUN SCHTASKS /Query /FO CSV /NH > &lcFile 

* Read the File
IF FILE(lcFile)
	lnFileHndl = FOPEN(lcFile, 10)
	
	*Check for error opening file
	IF lnFileHndl < 0  && 
		* Error Opening File Ignore
	ELSE
		lnSize =  FSEEK(lnFileHndl, 0, 2)     && Move pointer to EOF

		IF lnSize <= 0
			* File is Empty Ignore
		ELSE
			FSEEK(lnFileHndl, 0, 0)      && Move pointer to BOF

			DO WHILE ! FEOF(lnFileHndl)
				lcLine = ALLTRIM(FGETS(lnFileHndl))
			ENDDO
		ENDIF 
		
		IF (!FCLOSE(lnFileHndl))
			* Error Closing File
		ENDIF	
	ENDIF
ENDIF
 
You may look at faq184-4140 the section "Run a schedule task now, rather than wait for the scheduled time." Shows how to deal with a task.


Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
2020Tech,

I could have sworn you were looking for a way to create a scheduled task...

"Is it possible to create a scheduled task progmatically using foxpro 8.0SP1 on a windows XP machine."

...your example appears to just query a list of scheduled tasks, not actually create one. But if that's what you wanted, then I'm glad you found something that works for you.

You are correct though, the above code I posted creates AT.EXE jobs not MSTASK.EXE jobs.

boyd.gif

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top