简体   繁体   中英

Task that successfully scheduled and ran in Windows 7 could not run in Windows XP

I'm writing an application that will automatically schedule a task. It's supposed to work on both Windows 7 and Windows XP. Unfortunately, I can't seem to run it on Windows XP.

In the setup .C file, I create the batch file and execute it. On the Windows 7 version, the task command looks like this:

schtasks /create /tn \"<Script Name>\" /xml \"%s\\<Script XML>\"

Where %s is the working directory of the setup file (and the temporary batch script). This method has been tested on multiple Windows 7 machines and has been confirmed to work flawlessly on all of them. The reason that I decided to create the task using an XML file is because the task would not run on laptops, because by default the automatically-generated XML sets the script to NOT RUN on battery.

When I tried to run the same application on Windows XP, however, I got the error that the /xml parameter was not recognized, so I rewrote the task schedule command for XP. I knew that the XP computers this application would be running on were not laptops anyway, so I figured I didn't have to worry about the "don't run on battery" part of the auto-generated XML.

Here's the task command for the Windows XP version:

schtasks /create /tn \"<Script Name>\" /tr \"%s\\<File To Run>.exe\" /sc minute /mo 60

When the application finishes installing, I get the status message "Could not run" on the task that I schedule. I know that the executable that I'm scheduling DOES work, because it works when I run it manually (on both XP and Windows 7), so I'm wracking my brains trying to find a solution to this problem.

If it matters, the executable is a C# file. I logged the operating directory of the batch file and the C# executable, and they're both correct, so I'm not sure why I can't get this task to run automatically on Windows XP.

If anyone can help me, I'd really appreciate it. I've been working on this problem for a few days now.

Not having an XP machine or windows XP mode, this is all a stab in the dark.

My main prediction is that i don't think you have to escape the quotes in /tn \\"<Script Name>\\" because i got an error while testing it. The same probably goes for /tr \\"%s\\\\<File To Run>.exe\\" . Even if either of those have spaces in them, normal (un-escaped) quotes should do. I tested schtasks /Create /TN \\"name\\" /TR cmd /SC once /ST 12:00 in windows 7 and i got the error ERROR: The filename, directory name, or volume label syntax is incorrect. .
Another thing is that you have /SC set to minute , but you also have /MO set to 60 . I'd predict that that's fine on windows 7, but that might make XP complain. setting /SC to HOURLY might be worth a shot (if XP has that option).

As a final note, in general, windows utilities don't change massively between versions, especially if they don't display their version in the help file. It's unlikely that anything basic has changed since XP except for the /XML option (and also /V1 ). If it works in 7, there's about a 95% chance that it'll work in XP, so you might try testing stuff out there too.

As a side note, the escape character for CMD is ^ for seemingly all applications. schtasks /Create /TN ^"name^" /TR cmd /SC once /ST 13:00 and schtasks /Create /TN "name" /TR cmd /SC once /ST 13:00 both worked perfectly, although they did overwrite each other and just have the name name without the quotes (windows probably stripped the quotes before creating them).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM