Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly quote file path with SCHTASKS

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.

like image 621
Ducain Avatar asked Apr 29 '11 21:04

Ducain


3 Answers

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  
like image 143
mousio Avatar answered Sep 25 '22 10:09

mousio


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);
like image 24
gab Avatar answered Sep 25 '22 10:09

gab


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
like image 44
jedie Avatar answered Sep 22 '22 10:09

jedie