Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Scheduled Tasks


  • Please log in to reply
4 replies to this topic
rtmey2000
  • Members
  • 6 posts
  • Last active: Feb 19 2010 10:44 AM
  • Joined: 14 May 2007
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

SCHTASKS /Delete /TN "MyDailyBackup" /f

and from AHK it would look like:

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:
/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. )

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.o...7000/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)

jaco0646
  • Moderators
  • 3165 posts
  • Last active: Apr 01 2014 01:46 AM
  • Joined: 07 Oct 2006
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. :p

richard2121
  • Guests
  • Last active:
  • Joined: --
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

dvda2k
  • Members
  • 48 posts
  • Last active: Mar 12 2012 09:41 AM
  • Joined: 19 Apr 2007
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\"" ...


dvda2k
  • Members
  • 48 posts
  • Last active: Mar 12 2012 09:41 AM
  • Joined: 19 Apr 2007
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.