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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to run a scheduled task now rather then on schedule. 3

Status
Not open for further replies.

Mike Gagnon

Programmer
Apr 6, 2002
8,067
CA
Here is a quick example on forcing a scheduled task to run now, rather then on schedule, using "SHELL.APPLICATION".
Code:
ssfCONTROLS = 3  && Control Panel's Schedule Tasks folder
sJobName = "fta"  && Name of the task to run
sRunVerb = "R&un"  && Executing command
sEndVerb = "&End Task"  && Cancelling command
shellApp = Createobject("shell.application")
oControlPanel = shellApp.Namespace(ssfCONTROLS) && Schedule Tasks folder
oST = ''
For Each folderitem In oControlPanel.items  && Loop though the items in the Control Panel items
	If folderitem.Name  = "Scheduled Tasks"
		oST = folderitem.getfolder() && Found it
		Exit
	Endif
Next
If Vartype(oST) != 'O'
	Messagebox("Couldn't find 'TS' folder")
Endif
oJob = ''
For Each folderitem In oST.items  && Loop through the different scheduled tasks until we fiind it.
	If   Lower(folderitem.Name)  = Lower(sJobName) 
		oJob = folderitem  && Found it
		Exit
	Endif
Next
If Vartype(oJob) !="O"
	Messagebox( "Couldn't find " + sJobName + " item")
Else
	bEnabled = .T.
	oRunVerb = ''
	oEndVerb = ''
	s = "Verbs: " + Chr(13)
	For Each Verb In oJob.verbs  && Loop through the different commands in the scheduled task until we find right one.
		s = s + Chr(13) + Verb.Name
		If Verb.Name = sRunVerb
			oRunVerb = Verb
			bEnabled = .F.
		Endif
		If Verb.Name = sEndVerb
			oEndVerb = Verb
		Endif
	Next
	If bEnabled
		oJob.InvokeVerb(oEndVerb)  && Cancel the task
	Else
		Wait Window Nowait "executing job"
		oJob.InvokeVerb(sRunVerb)  && Run the task!
	Endif
Endif




Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Nice work Mike. Star Worthy!

Slighthaze = NULL
craig1442@mchsi.com
"Whom computers would destroy, they must first drive mad." - Anon​
 
slighthaze

Thanks for the star, but what's nice this time I actually needed a function like that, rather then another member asking for it.

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Yep -
star.gif


Can you include it as an FAQ?

Jim Osieczonek
Delta Business Group, LLC
 

Jim

Can you include it as an FAQ?

I put it as part of faq184-4140

Chris

Have yet to try this code but is there a typo here?

sRunVerb = "R&un"


No, and neither is sEndVerb = "&End Task". You can actually see all the commands if you put a wait window at this line:

Code:
 s = "Verbs: " + Chr(13)
    For Each Verb In oJob.verbs
        s = s + Chr(13) + Verb.Name
        If Verb.Name = sRunVerb
            oRunVerb = Verb
            bEnabled = .F.
        Endif
        If Verb.Name = sEndVerb
            oEndVerb = Verb
        Endif
    Next
    [b]wait window s [/b]

You'll notice that each verb contains a "&" somewhere in it.



Mike Gagnon

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

Someone recently was asking about starting a dialup connection through VFP, and I cannot find the thread put with a couple a line changes in the above code you can achieve that (Here is an example that brings up my AOL connection)
Code:
ssfCONTROLS = 3  && Control Panel's 
sJobName = "AOL"  && Name of the task to run
sRunVerb = "C&onnect"  && Executing command
shellApp = Createobject("shell.application")
oControlPanel = shellApp.Namespace(ssfCONTROLS) && Schedule Tasks folder
oST = ''
For Each folderitem In oControlPanel.items  && Loop though the items in the Control Panel items
    If folderitem.Name  = "Network Connections"
        oST = folderitem.getfolder() && Found it
        Exit
    Endif
Next
If Vartype(oST) != 'O'
    Messagebox("Couldn't find 'TS' folder")
Endif
oJob = ''
For Each folderitem In oST.items  && Loop through the different scheduled tasks until we fiind it.
    If   Lower(folderitem.Name)  = Lower(sJobName) 
        oJob = folderitem  && Found it
        Exit
    Endif
Next
If Vartype(oJob) !="O"
    Messagebox( "Couldn't find " + sJobName + " item")
Else
    bEnabled = .T.
    oRunVerb = ''
    oEndVerb = ''
    s = "Verbs: " + Chr(13)
    For Each Verb In oJob.verbs  && Loop through the different commands in the scheduled task until we find right one.
        If Verb.Name = sRunVerb
            oRunVerb = Verb
            bEnabled = .F.
        Endif
        If Verb.Name = sEndVerb
            oEndVerb = Verb
        Endif
    Next
    If bEnabled
        oJob.InvokeVerb(oEndVerb)  && Cancel the task
    Else
        Wait Window Nowait "executing job"
        oJob.InvokeVerb(sRunVerb)  && Run the task!
    Endif
Endif

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
I would suggest adjusting the assignments for:
sRunVerb = "R&un"
sEndVerb = "&End Task"
sRunVerb = "C&onnect"

to something like:
sRunVerb = "R&"+"un"
sEndVerb = "&"+"End Task"
sRunVerb = "C&"+"onnect"

otherwise Macro Replacement might kick in if there happens to exist a variable called "un" or "end" or "onnect"

Which may never exist because of your own naming standards, but you never know what environment a common routine will be run in.
 
I see no possbile way for VFP to interpret the following lines as containing macro substitution given that the & symbol is contained within string delimiters.

Local un, end, onnect
Store "MessageBox('Kicked In!')" to un, end, onnect
sRunVerb = "R&un"
sEndVerb = "&End Task"
sRunVerb = "C&onnect"

...I believe that splitting the strings up as suggested is unnecessary. Barring a typo, i just don't see how it could happen.

Slighthaze = NULL
craig1442@mchsi.com
"Whom computers would destroy, they must first drive mad." - Anon​
 
Try this simple example:
Code:
xt = 'Gotcha!'
yy = 'te&xt'
?"yy=",yy
 
SLIGHTHAZE AND WGCS

I agree with slighthaze, the command that oJob.InvokeVerb(sRunVerb) is expecting is "R&un", it is not a macro substitution, but an actual command. I do not see the need to concactinate the sRunVerb.

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Mike: Run the example....

quotes are often suggested as a way to take care of long file names, while a better way is a name expression, but quotes with macro expansion does work:

cFilename = "c:\Documents and Settings\Username\My Long File Name.dbf"
COPY TO "&cFileName"
* the macro expansion still works inside the quotes!

Slighthaze's code will probably work in all cases where it is used, ever, but I just wanted to caution everyone about writing utility routines that include macro-expansion syntax that isn't supposed to be expanded, that potentially could crop up as a bug.
 
wgcs,

I stand corrected. [blush]

Star for opening my eyes, I shall not soon forget that very nasty VFP behavior. I am assuming that MS has been aware of this for some time?

Slighthaze = NULL
craig1442@mchsi.com
"Whom computers would destroy, they must first drive mad." - Anon​
 
I think its designed in.... I've heard about it for a long time, but I remember when I first encountered it I had the same reaction: "No, That can't be... Not That!!! Oh Well..."
 
wgcs

With all this said, I still maintain my point, the actual command inside the controlpanel->network connection is "R&un". Do forget the code suggested is not VFP per se but rather Window Scripting. But I understand that macro expansion in VFP may act funny at times, but this isn't macro expansion.

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
I understand. Macro replacement isn't intended in this instance, the string is intended to have an ampersand in it.

However VFP's macro replacement makes any use of "&" inside a string dangerous unless it has no characters to the right of the "&", so I make it a practice to never allow it unless macro replacement is intended, and to always use the construction: "string &"+"string continuation" to avoid it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top