AutoHotkey Community

It is currently May 26th, 2012, 11:37 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Scheduled Tasks
PostPosted: March 13th, 2008, 5:15 pm 
Offline

Joined: May 14th, 2007, 10:39 pm
Posts: 6
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)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 14th, 2008, 1:02 am 
Offline

Joined: October 7th, 2006, 4:50 pm
Posts: 3157
Location: MN, USA
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 17th, 2008, 3:17 pm 
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


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: November 18th, 2008, 6:20 am 
Offline

Joined: April 19th, 2007, 8:05 am
Posts: 48
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\"" ...


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 18th, 2008, 7:42 am 
Offline

Joined: April 19th, 2007, 8:05 am
Posts: 48
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.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Google [Bot], Rajat and 52 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group