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.
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
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);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With