8

I'm writing a small app that generates the contents of a batch file, using SCHTASKS to create scheduled tasks. However, I simply cannot get the file path working correctly. I need another set of eyes.

SCHTASKS /CREATE /TN "TASK1" /TR "\"C:\Program_Files\Spybot - Search & Destroy\SpybotSD.exe\" \AUTOCHECK \AUTOFIX \AUTOCLOSE" /ST 01:00:00 /SC Daily /RU MyUser /RP MyPass 

I've looked at other threads here, and MS documentation, and I think I have that formed correctly. However, it fails with the output:

ERROR: Invalid syntax. Mandatory option '/sc' is missing.
Type "SCHTASKS /CREATE /?" for usage.
The system cannot find the path specified.

I could use some advice here.

Ducain
  • 1,521
  • 3
  • 17
  • 27

3 Answers3

9

You need to escape the ampersand with a caret like this:

SCHTASKS /CREATE /TN "TASK1" /TR "\"C:\Program_Files\Spybot - Search ^& Destroy\SpybotSD.exe\" \AUTOCHECK \AUTOFIX \AUTOCLOSE" /ST 01:00:00 /SC Daily /RU MyUser /RP MyPass  
mousio
  • 9,361
  • 4
  • 30
  • 40
  • On my machine (Windows 7), while the caret did help to get the task registered, it did not actually run. However, adding the `/V1` switch made it all work as expected :] – mousio May 01 '11 at 21:31
  • Just place the switch at the appropriate position as outlined in the help description. `schtasks /create /?` gives me the following (in short): `SCHTASKS /Create [/S system [/U username [/P [password]]]] [/RU username [/RP password]] /SC schedule [/MO modifier] [/D day] [/M months] [/I idletime] /TN taskname /TR taskrun [/ST starttime] [/RI interval] [ {/ET endtime | /DU duration} [/K] [/XML xmlfile] [/V1]] [/SD startdate] [/ED enddate] [/IT | /NP] [/Z] [/F]` – mousio Jun 29 '11 at 13:57
3

I came across with this problem and the way I solved it was changing the order in the options for creating the schtask as is explained in this post http://billaking.blogspot.com/2010/11/c-sharp-windows-task-with-schtasks.html it worked just perfect.

StringBuilder commandLineParams = new StringBuilder();
commandLineParams.AppendFormat("/Create /RU SYSTEM /SC {0} /ST {1} /TN {2} /TR \"\\\"{3}\\\"", strScheduleType.ToUpper(), intTimeInterval, strTaskName, strProgramPath);
jeb
  • 70,992
  • 15
  • 159
  • 202
gab
  • 225
  • 1
  • 11
1

I would like to use %ProgramFiles(x86)%

My solution:

set taskrun="%ProgramFiles(x86)%\foo\bar.exe"
schtasks /create /TN FooBar /TR \"%taskrun%\" /SC ONLOGON /RL HIGHEST /F
jedie
  • 1,523
  • 2
  • 10
  • 7
  • schtasks /Create /SC DAILY /TN abc /TR "\"C:\Documents and Settings\All Users\Desktop\XXX.exe\"" /ST 17:46:00. Help me with tis task. its not running – Raj Jun 29 '11 at 10:59
  • @ZapSwami: did the task get registered? Are the permissions/credentials OK? (exe is under All Users) Does the [`/V1` switch](http://stackoverflow.com/questions/5837761/schtasks-how-to-properly-quote-file-path/5850720#5850720) help? – mousio Jun 29 '11 at 14:33