views:

349

answers:

5

We are scheduling a task programatically. However, the executable to be scheduled could be installed in a path that has spaces. ie c:\program Files\folder\folder\folder program\program.exe

When we provide this path as a parameter to the Tasjk Scheduler it fails to start because it cannot find the executable. It obviously needs to be enclosed in quotes (").

The problem we are having is that even when we enclosed the path in quotes when we pass it as a paramemter (cmd + "\" + path + "\") it still doesnt include the quotes in the path that is used to schedule the task.

Anyone have any idea how to force the quotes to be included in the path?

EDIT: Answer to comment:

We had the same idea, and here is the problem. the ~1 format is based on the index of the folder, so if say you had these 3 folders:

Program Applications
Program Files
Program Zips

then the path would be: progra~2

Now if you say there are over 10 of those folders, the path could possibly look like: progr~12.

Now, not to say this is not a viable solution, but having to count the folders to find the right one and then use the index to build the path is a little cumbersome and not very clean IMO.

We are hoping there is a better way.

EDIT 2: Added applicable code snippet

You asked for the code: this is how we build the Args string that we pass to the scheduler:

string args = "/CREATE /RU SYSTEM /SC " + taskSchedule + " /MO " + taskModifier + " /SD " + taskStartDate + " /ST " + taskStartTime + " /TN " + taskName + " /TR \"" + taskSource + "\"";

where taskSource is the path to the application.

A: 

you could replace program files with progra~1
and folder program to folder~1 (1st 6 letters and ~1) to get it to work till someone posts the right answer

adi92
See the edit on the question. not enough space here to explain.
Victor
A: 
Tim
thanks for the link. It actually raises a couple of issue with using the short name format. according to the link, the short names of the folders are generated at creation, and if a folder is deleted, the other folders short names dont change. which means my calculation might be wrong.
Victor
furthermore, depending on the OS installation, the short names might not be generated, which then would cause a failure as the executable would not be found. this is worse than i had thought.
Victor
A: 

Guessing the short name is not a good way since it's an implementation detail and can change with every windows version.

If you need the short name, just ask for it: http://www.c-sharpcorner.com/UploadFile/crajesh1981/RajeshPage103142006044841AM/RajeshPage1.aspx

VVS
thanks for the link. However we could possibly run into a situation, where: "When an application calls this function and specifies a path on a volume that does not support 8.3 aliases, the function fails with ERROR_INVALID_PARAMETER if the path is longer than 67 bytes."
Victor
+2  A: 

It appears that you're using schtasks.exe - it took me longer to figure that out than to find an answer! More details please! :) I found an answer with a quick google search

Try this code:

string args = "/CREATE /RU SYSTEM /SC " + taskSchedule + " /MO " + taskModifier + " /SD " + taskStartDate + " /ST " + taskStartTime + " /TN " + taskName + " /TR \"\\\"" + taskSource + "\""

It's adding a \" to the front of the TR parameter value.

TheSoftwareJedi
It worked, thanks!
Victor
Google is your friend. Google first, SO second. :)
TheSoftwareJedi
This probably would have been more obvious as a string.Format(@""). I like string.Format().
recursive
Google first, SO second, TheSoftwareJedi third
ScottCher
Well, i could have sworn we tried it this way, but we didnt, we had escaped quotes twice, we didnt add the escape character as well. FWIW, the link wasnt as helpful as your answer, so i think SO would be first, Google 2nd. Thats why SO was created, so we get meaningful targeted answers.thanks again!
Victor
A: 

Put a batch file in a location that does not have spaces.

In the batch file, run the program commands that have spaces.

recursive
Heck yeah, renaming your executable is a simple solution to a simple problem. It adds a level of complexity and confusion some years down the line when something changes, but what are you gonna do?
Karl
I'm not advocating renaming any executables. Just create a new one line batch file. You can even put comments in it to explain the situation.
recursive