| View previous topic :: View next topic |
| Author |
Message |
rtmey2000
Joined: 14 May 2007 Posts: 6
|
Posted: Thu Mar 13, 2008 4:15 pm Post subject: Scheduled Tasks |
|
|
I had a great deal of trouble scheduling tasks from AHK. Just wanted to share/document what I have learned.
I used SCHTASKS.EXE. There is reference material located here:http://www.ss64.com/nt/schtasks.html and http://support.microsoft.com/kb/814596.
SCHTASKS has several oddities that require work arounds.
First Issue: Deleting old tasks.
Deleting old tasks. SCHTASKS requires that the task name be within quotes while deleting (it does not require the same for creating new tasks). So you have to submit the following
| Code: | | SCHTASKS /Delete /TN "MyDailyBackup" /f |
and from AHK it would look like:
| Code: | DelTask:="SCHTASKS.exe /Delete /TN ""TaskName"" /f"
Runwait, %DelTask%,,hide |
Second Issue: Add new Tasks
Adding new tasks has some complexities. First you need to provide a windows username/password and domain. The second and more problematic issue is that it does not handle long paths/filesnames easily. Quotes around the long file name do not work. One simple way to circumvent this is to use a LOOP FILENAME, and convert a long filename to a short filename. The other way to circumvent this problem is to place enter the following after TaskRun option:
| Code: | | /TR "\"C:\Documents and Settings\My Documents\folder\script.ahk" |
Here is the code:
(This part of the script runs on first run only, and it schedules itself to run at the time you designate every weekday. See the SCHTASKS ref above to change the frequency that the scheduled tasks runs.Change the A_ScriptFullPath to another variable/path if you want to schedule another script/activity. )
| Code: |
Sch_Time := ;Time in 24 hr format ---> hh:mm:ss
SCh_UserName := ;Windows Username
SCh_Password := ;Windows Password
SCh_Domain := ;Windows Domain
DelTask:="SCHTASKS.exe /Delete /TN ""ScheduledTask"" /f"
Task1:="SCHTASKS.exe /Create /SC weekly /D MON,TUE,WED,THU,FRI /TN ScheduledTask /ST " . Sch_Time . " /TR ""\""" . A_ScriptFullPath . """" . " /RU " . SCh_Domain . "\" . SCh_UserName . " /RP " . SCh_Password
Runwait, %DelTask%,,hide
Runwait, %Task1%,,hide
|
The only reference I could find to SCHTASKS not accepting long paths was the following: http://web.archive.org/web/20060215004648/http://www.jsifaq.com/SUBO/tip7000/rh7035.htm
SCHTASKS also keeps a log file. This is useful for debugging. This can be accessed at the Start/Run box by entering: notepad c:\windows\schedlgu.txt
Hopes this helps some of you.
(FYI, I am using a non-admin account on XP. Have not tried this on other versions of windows) |
|
| Back to top |
|
 |
jaco0646
Joined: 07 Oct 2006 Posts: 3111 Location: MN, USA
|
Posted: Fri Mar 14, 2008 12:02 am Post subject: |
|
|
Interesting. Scheduling tasks is a common question in the Ask for Help forum, and I think SetTimer often gets used because people overlook or underestimate Windows' built-in feature. I may start directing them to this topic.  |
|
| Back to top |
|
 |
richard2121 Guest
|
Posted: Mon Nov 17, 2008 2:17 pm Post subject: |
|
|
Hi
I'm trying to do the same except with a usb stick. Could someone do a example, like spybot run every Wenesday at 7pm hotdogADMIN and starfishPWD, PC unknown and many
Regards |
|
| Back to top |
|
 |
dvda2k
Joined: 19 Apr 2007 Posts: 45
|
Posted: Tue Nov 18, 2008 5:20 am Post subject: |
|
|
Thanks the information on schtasks.exe
FYI, schtasks.exe does accept path/filenames with space. Type schtasks /create /? and you can find an example at the very end of help:
SCHTASKS /Create
/tr "'c:\program files\internet explorer\iexplorer.exe'
\"c:\log data\today.xml\"" ... |
|
| Back to top |
|
 |
dvda2k
Joined: 19 Apr 2007 Posts: 45
|
Posted: Tue Nov 18, 2008 6:42 am Post subject: |
|
|
The annoying part is schtasks.exe just won't let you fully control scheduled tasks like taskschd.msc (GUI) does.
For example, you can't change the default setting "Start task only if on AC power" by schtasks.exe. To do that, you'll have to open the taskschd.msc and untick the check box.
Do you guys know any workaround? So I can fully control tasks from command line. |
|
| Back to top |
|
 |
|